Skip to content

Commit 6726bb4

Browse files
author
lichuang
committed
learning_tensorflow
1 parent 22f2148 commit 6726bb4

File tree

6 files changed

+1689
-2
lines changed

6 files changed

+1689
-2
lines changed

chatbotv2/lstm_train.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def create_model(max_word_id, is_test=False):
7676
decoder_inputs = tf.slice(network, [0, max_seq_len], [-1, max_seq_len], name="dec_in")
7777
decoder_inputs = tf.unpack(decoder_inputs, axis=1)
7878
go_input = tf.mul( tf.ones_like(decoder_inputs[0], dtype=tf.int32), GO_VALUE )
79-
decoder_inputs = [go_input] + decoder_inputs[: max_seq_len-1]
79+
decoder_inputs = [go_input] + decoder_inputs[: max_max_seq_len-1]
8080
num_encoder_symbols = max_word_id + 1 # 从0起始
8181
num_decoder_symbols = max_word_id + 2 # 包括GO
8282

@@ -96,7 +96,7 @@ def create_model(max_word_id, is_test=False):
9696

9797

9898

99-
targetY = tf.placeholder(shape=[None, max_seq_len], dtype=tf.int32, name="Y")
99+
targetY = tf.placeholder(shape=[None, max_seq_len], dtype=tf.float32, name="Y")
100100

101101
network = tflearn.regression(
102102
network,

learning_tensorflow/1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import tensorflow as tf
2+
3+
sess = tf.Session()
4+
5+
a = tf.placeholder("float")
6+
b = tf.placeholder("float")
7+
c = tf.constant(6.0)
8+
d = tf.mul(a, b)
9+
y = tf.mul(d, c)
10+
print sess.run(y, feed_dict={a: 3, b: 3})
11+
12+
A = [[1.1,2.3],[3.4,4.1]]
13+
Y = tf.matrix_inverse(A)
14+
print sess.run(Y)
15+
sess.close()

learning_tensorflow/2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import tensorflow as tf
4+
5+
with tf.Graph().as_default() as g:
6+
with g.name_scope("myscope") as scope: # 有了这个scope,下面的op的name都是类似myscope/Placeholder这样的前缀
7+
sess = tf.Session(target='', graph = g, config=None) # target表示要连接的tf执行引擎
8+
print "graph version:", g.version # 0
9+
a = tf.placeholder("float")
10+
print a.op # 输出整个operation信息,跟下面g.get_operations返回结果一样
11+
print "graph version:", g.version # 1
12+
b = tf.placeholder("float")
13+
print "graph version:", g.version # 2
14+
c = tf.placeholder("float")
15+
print "graph version:", g.version # 3
16+
y1 = tf.mul(a, b) # 也可以写成a * b
17+
print "graph version:", g.version # 4
18+
y2 = tf.mul(y1, c) # 也可以写成y1 * c
19+
print "graph version:", g.version # 5
20+
operations = g.get_operations()
21+
for (i, op) in enumerate(operations):
22+
print "============ operation", i+1, "==========="
23+
print op # 一个结构,包括:name、op、attr、input等,不同op不一样
24+
assert y1.graph is g
25+
assert sess.graph is g
26+
print "================ graph object address ================"
27+
print sess.graph
28+
print "================ graph define ================"
29+
print sess.graph_def
30+
print "================ sess str ================"
31+
print sess.sess_str
32+
print sess.run(y1, feed_dict={a: 3, b: 3}) # 9.0 feed_dictgraph中的元素和值的映射
33+
print sess.run(fetches=[b,y1], feed_dict={a: 3, b: 3}, options=None, run_metadata=None) # 传入的feches和返回值的shape相同
34+
print sess.run({'ret_name':y1}, feed_dict={a: 3, b: 3}) # {'ret_name': 9.0} 传入的feches和返回值的shape相同
35+
36+
assert tf.get_default_session() is not sess
37+
with sess.as_default(): # 把sess作为默认的session,那么tf.get_default_session就是sess, 否则不是
38+
assert tf.get_default_session() is sess
39+
40+
h = sess.partial_run_setup([y1, y2], [a, b, c]) # 分阶段运行,参数指明了feches和feed_dict列表
41+
res = sess.partial_run(h, y1, feed_dict={a: 3, b: 4}) # 12 运行第一阶段
42+
res = sess.partial_run(h, y2, feed_dict={c: res}) # 144.0 运行第二阶段,其中使用了第一阶段的执行结果
43+
print "partial_run res:", res
44+
sess.close()

learning_tensorflow/3.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
import tensorflow as tf
5+
6+
# 随机生成1000个点,围绕在y=0.1x+0.3的直线周围
7+
num_points = 4
8+
vectors_set = []
9+
for i in xrange(num_points):
10+
x1 = np.random.normal(0.0, 0.55)
11+
y1 = x1 * 0.1 + 0.3 + np.random.normal(0.0, 0.03)
12+
vectors_set.append([x1, y1])
13+
14+
# 生成一些样本
15+
x_data = [v[0] for v in vectors_set]
16+
y_data = [v[1] for v in vectors_set]
17+
print "x_data=", x_data
18+
19+
20+
# 生成1维的W矩阵,取值是[-1,1]之间的随机数
21+
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='W')
22+
# 生成1维的b矩阵,初始值是0
23+
b = tf.Variable(tf.zeros([1]), name='b')
24+
# 经过计算得出预估值y
25+
y = W * x_data + b
26+
print "y=", y
27+
28+
# 以预估值y和实际值y_data之间的均方误差作为损失
29+
loss = tf.reduce_mean(tf.square(y - y_data), name='loss')
30+
# 采用梯度下降法来优化参数
31+
optimizer = tf.train.GradientDescentOptimizer(0.5)
32+
# 训练的过程就是最小化这个误差值
33+
train = optimizer.minimize(loss, name='train')
34+
35+
sess = tf.Session()
36+
# 输出图结构
37+
#print sess.graph_def
38+
39+
init = tf.initialize_all_variables()
40+
sess.run(init)
41+
42+
# 初始化的W和b是多少
43+
print "W =", sess.run(W), "b =", sess.run(b), "loss =", sess.run(loss)
44+
# 执行20次训练
45+
for step in xrange(20):
46+
sess.run(train)
47+
# 输出训练好的W和b
48+
print "W =", sess.run(W), "b =", sess.run(b), "loss =", sess.run(loss)
49+
# 生成summary文件,用于tensorboard使用
50+
writer = tf.train.SummaryWriter("./tmp", sess.graph)

0 commit comments

Comments
 (0)