In [6]:
import tensorflow as tf
import numpy as np

In [2]:
import time
time.strftime('%m%d',time.localtime(time.time()))


Out[2]:
'0601'

In [18]:
import pandas as pd

data = pd.read_excel('/home/jeffmxh/IMG_Inception/mid_end_incp.xlsx')
dict(data.groupby('prediction').size())

import os
os.system('ls -hl')


Out[18]:
0

In [23]:
import re
data = pd.read_excel('/home/jeffmxh/IMG_Inception/mid_end_incp.xlsx')
def re_blank(x):
    x = re.sub(', ', ',', x)
    x = re.sub(' ', '_', x)
    return x
data['prediction'] = data['prediction'].apply(re_blank)
result = list(data.prediction)

In [31]:
from pickle import load
import re

In [32]:
data = load(open("/home/da/nlp/ltp_test/seq2seq_inputs.pkl", 'rb'))

In [37]:
data.shape[0]/1024*50


Out[37]:
14133.69140625

In [18]:
content = data.data
label = data.label

In [33]:
content_str = [' '.join(a) + '\n' for a in content]
label_str = [' '.join(a) + '\n' for a in content]

content_train = content_str[0:250000]
content_dev = content_str[250000:]
label_train = label_str[0:250000]
label_dev = label_str[250000:]

In [34]:
content_train[0]


Out[34]:
'人 们 常 说 生 活 是 一 部 教 科 书\n'

In [35]:
with open('Jeffmxh/content_train.txt', 'w') as f:
    f.writelines(content_train)
with open('Jeffmxh/content_dev.txt', 'w') as f:
    f.writelines(content_dev)
with open('Jeffmxh/label_train.txt', 'w') as f:
    f.writelines(label_train)
with open('Jeffmxh/label_dev.txt', 'w') as f:
    f.writelines(label_dev)

In [4]:
s = 'asdfa\000sfwafwf'

import re
ILLEGAL_CHARACTERS_RE = re.compile(r'[\000-\010]|[\013-\014]|[\016-\037]')
m = ILLEGAL_CHARACTERS_RE.sub('', s)
m


Out[4]:
'asdfasfwafwf'

In [2]:
import os
import re
a = os.listdir('/home/jeffmxh/shiny_shape/',)
pattern = re.compile('.+?.jpg$')
pattern.match(a[0])
list(filter(pattern.match,a))


Out[2]:
['猫.jpg', '人物1.jpg']

In [4]:
class info():
    def __init__(self, name):
        self.name = name
    @property
    def show_name(self):
        return 'Name : ' + str(self.name)
    @staticmethod
    def show_params():
        print('This is staticmethod')

In [5]:
a = info('Jeff')
print(a.show_name)
info.show_params()
print(isinstance(a, info))


Name : Jeff
This is staticmethod
True

In [ ]:
def log(func):
    def _log(a, b):
        print("before myfunc() called.")
        ret = func(a, b)
        print("  after myfunc() called. result: %s" % ret)
        return ret
    return _log

@log
de

In [33]:
elems = np.array([1, 2, 3, 4, 5, 6])
initializer = np.array(0)
sum_one = tf.scan(
    lambda a, x: x[0] + x[1] + a, (elems + 1, elems), initializer)
with tf.Session() as sess:
    result = sess.run(sum_one)
    print(result)

# f = lambda a, x: x[0] - x[1] + a
(elems + 1, elems)


[ 3  8 15 24 35 48]
Out[33]:
(array([2, 3, 4, 5, 6, 7]), array([1, 2, 3, 4, 5, 6]))

In [48]:
import time
print(time.asctime( time.localtime(time.time()) ))


Mon May  8 17:49:40 2017

In [46]:
elems = np.array([0, 0, 0, 0, 0, 0])
initializer = (np.array(1), np.array(2))
fibonaccis = tf.scan(lambda a, _: (a[1], a[0] + a[1]), elems, initializer)
with tf.Session() as sess:
    result = sess.run(fibonaccis)
    print(result)
initializer


(array([ 2,  3,  5,  8, 13, 21]), array([ 3,  5,  8, 13, 21, 34]))
Out[46]:
(array(1), array(2))

In [2]:
matrix1 = tf.constant([[3.,3.]])
matrix2 = tf.constant([[2.], [2.]])
product = tf.matmul(matrix1, matrix2)

In [6]:
with tf.Session() as sess:
    result = sess.run(product)
    print(result)


[[ 12.]]

In [10]:
ckpt = tf.train.get_checkpoint_state('/home/jeffmxh/deepnlp/deepnlp/textsum/ckpt/')
if not ckpt:
    print("aaa")


aaa

In [11]:
x_data = np.random.rand(100).astype('float32')
y_data = x_data*0.1+0.3

In [12]:
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b


init = tf.global_variables_initializer()

In [13]:
# 最小化均方误差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

In [14]:
with tf.Session() as sess:
    sess.run(init)
    for step in range(200):
        sess.run(train)
        if step % 20 == 0:
            print(step, sess.run(W), sess.run(b))


0 [-0.11474532] [ 0.56045181]
20 [ 0.03648885] [ 0.333269]
40 [ 0.08536214] [ 0.30766776]
60 [ 0.09662631] [ 0.30176726]
80 [ 0.09922244] [ 0.30040732]
100 [ 0.09982079] [ 0.30009389]
120 [ 0.0999587] [ 0.30002165]
140 [ 0.09999049] [ 0.30000499]
160 [ 0.09999783] [ 0.30000114]
180 [ 0.09999949] [ 0.30000028]

In [15]:
# 使用 NumPy 生成假数据(phony data), 总共 100 个点.
x_data = np.float32(np.random.rand(2, 100)) # 随机输入
y_data = np.dot([0.100, 0.200], x_data) + 0.300

In [16]:
y_data


Out[16]:
array([ 0.48396323,  0.55351732,  0.33899472,  0.45908227,  0.53261669,
        0.44104615,  0.45141883,  0.37340933,  0.58868018,  0.41954356,
        0.3833061 ,  0.40072405,  0.36961595,  0.53800362,  0.43245118,
        0.53928325,  0.51903484,  0.45086404,  0.44299852,  0.47614282,
        0.43604107,  0.33730391,  0.33557073,  0.51708171,  0.38661739,
        0.34836774,  0.45860716,  0.42262529,  0.38884495,  0.31223386,
        0.49021673,  0.37656849,  0.40752567,  0.4524235 ,  0.44582348,
        0.36427148,  0.47381006,  0.38229752,  0.44348624,  0.31816132,
        0.45509555,  0.55592707,  0.42488478,  0.46627399,  0.49724117,
        0.51874799,  0.4561199 ,  0.44105923,  0.40547986,  0.49176467,
        0.42549955,  0.43657945,  0.49743639,  0.41057366,  0.48216227,
        0.48234096,  0.31174661,  0.51014954,  0.47386357,  0.37280035,
        0.35407241,  0.42714241,  0.44660879,  0.53342243,  0.35874878,
        0.55587199,  0.50195839,  0.46093298,  0.37758727,  0.51676001,
        0.4721247 ,  0.47539759,  0.53514807,  0.32375701,  0.3555939 ,
        0.53358845,  0.51354306,  0.32650626,  0.45100409,  0.47823924,
        0.41189586,  0.42173919,  0.449178  ,  0.38744913,  0.39603219,
        0.5598326 ,  0.49919308,  0.36473311,  0.4776157 ,  0.46165802,
        0.52579618,  0.36022347,  0.48049191,  0.44674552,  0.41635737,
        0.41467239,  0.39587797,  0.43973714,  0.3859603 ,  0.38144966])

In [17]:
# 构造一个线性模型
# 
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b

In [18]:
# 最小化方差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

In [19]:
# 初始化变量
init = tf.global_variables_initializer()

# 启动图 (graph)
with tf.Session() as sess:
    sess.run(init)
    for step in range(200):
        sess.run(train)
        if step % 20==0:
            print(step, sess.run(W), sess.run(b))


0 [[ 0.12357616  0.57576615]] [ 0.17833456]
20 [[ 0.12670037  0.28080222]] [ 0.24634533]
40 [[ 0.11285964  0.22072469]] [ 0.28331554]
60 [[ 0.10477415  0.20576234]] [ 0.29477823]
80 [[ 0.10162169  0.20169124]] [ 0.29836023]
100 [[ 0.10053009  0.20051278]] [ 0.29948413]
120 [[ 0.10017014  0.20015833]] [ 0.29983759]
140 [[ 0.10005412  0.20004936]] [ 0.29994884]
160 [[ 0.10001714  0.20001547]] [ 0.29998389]
180 [[ 0.10000542  0.20000488]] [ 0.29999492]