Test1 常量、变量、Fetch、Feed、Session
在tensorflow中,常量通过constant函数来定义。 在tensorflow中,变量通过Variable函数来定义,注意:只要用到变量,就一定要run一下initialize_all_variables()来初始化变量。 Session在tensorflow中就像指针一样,指向需要运行的代码。 Session运行结束后,需要关闭。 Fetch对多个操作节点取值。 Feed给占位符赋值。
import tensorflow
as tf
a = tf.constant(
3.0)
b = tf.Variable(
1.0)
c = tf.placeholder(tf.float32)
d1 = tf.add(a,b)
d2 = tf.sub(a,b)
d3 = tf.mul(a,b)
d4 = tf.div(a,b)
e1 = tf.mod(a,b)
e2 = tf.abs(c)
e3 = tf.sqrt(c)
e4 = tf.pow(a,b)
f1 = tf.maximum(a,b)
f2 = tf.minimum(a,b)
m1 = tf.constant([[
3,
3]])
m2 = tf.constant([[
2],[
3]])
g = tf.matmul(m1,m2)
with tf.Session()
as sess:
sess.run(tf.global_variables_initializer())
result = sess.run([d1,d2,d3,d4,e1,e4,f1,f2,g])
print(result)
print (sess.run(e2, feed_dict={c: -
3}))
print (sess.run(e3, feed_dict={c:
4}))
print(g)
print(sess.run(g))
print(sess.run([g]))
123456789101112131415161718192021222324252627282930313233
运行结果:
[
4.0,
2.0,
3.0,
3.0,
0.0,
3.0,
3.0,
1.0, array(
[[15]])]
3.0
2.0
Tensor(
"MatMul:0", shape=(
1,
1), dtype=int32)
[[15]]
[array(
[[15]])]
1234567
Test2 循环
import tensorflow
as tf
state = tf.Variable(
0)
new_state = tf.add(state,
1)
update =tf.assign(state,new_state)
with tf.Session()
as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(state))
for _
in range(
5):
sess.run(update)
print(sess.run(state))
12345678910111213141516
运行结果:
0
1
2
3
4
5
123456
Test3 简单实例
import tensorflow
as tf
import numpy
as np
x_data = np.random.rand(
100)
y_data = x_data*
0.1+
0.2
b = tf.Variable(
0.)
k = tf.Variable(
0.)
y = k*x_data + b
loss = tf.reduce_mean(tf.square(y_data-y))
optimizer = tf.train.GradientDescentOptimizer(
0.2)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
with tf.Session()
as sess:
sess.run(init)
for step
in range(
201):
sess.run(train)
if step%
20 ==
0:
print(step,sess.run([k,b]))
1234567891011121314151617181920212223242526272829303132333435363738
运行结果:
0 [
0.047159102,
0.097865961]
20 [
0.098292276,
0.20082079]
40 [
0.099039868,
0.2004616]
60 [
0.099460147,
0.20025954]
80 [
0.099696465,
0.20014593]
100 [
0.099829316,
0.20008205]
120 [
0.099904031,
0.20004614]
140 [
0.099946037,
0.20002595]
160 [
0.09996964,
0.20001459]
180 [
0.099982917,
0.20000821]
200 [
0.099990398,
0.20000462]
1234567891011
可以看到k接近0.1、b接近0.2 关于reduce_mean()的博文: tensorflow学习之常用函数总结:tensorflow官方例子中的诸如tf.reduce_mean()这类函数
Test4 线性回归
import tensorflow
as tf
import numpy
as np
import matplotlib.pyplot
as plt
x_data = np.linspace(-
0.5,
0.5,
200)[:,np.newaxis]
noise = np.random.normal(
0,
0.02,x_data.shape)
y_data = np.square(x_data) + noise
x = tf.placeholder(tf.float32, [
None,
1])
y = tf.placeholder(tf.float32, [
None,
1])
Weights_L1 = tf.Variable(tf.random_normal([
1,
10]))
biases_L1 = tf.Variable(tf.zeros([
1,
10]))
Wx_plus_b_L1 = tf.matmul(x, Weights_L1) +biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)
Weights_L2 = tf.Variable(tf.random_normal([
10,
1]))
biases_L2 = tf.Variable(tf.zeros([
1,
1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2)
loss = tf.reduce_mean(tf.square(y-prediction))
train_step = tf.train.GradientDescentOptimizer(
0.1).minimize(loss)
with tf.Session()
as sess:
sess.run(tf.global_variables_initializer())
for _
in range(
2000):
sess.run(train_step, feed_dict={x:x_data, y:y_data})
prediction_value = sess.run(prediction, feed_dict={x:x_data})
plt.figure()
plt.scatter(x_data, y_data)
plt.plot(x_data,prediction_value,
'r-',lw=
5)
plt.show()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
运行结果: