美文网首页
Deep Learning with Tensorlfow:ch

Deep Learning with Tensorlfow:ch

作者: csuhan | 来源:发表于2019-02-19 21:15 被阅读0次
import tensorflow as tf
#阶
scalar = tf.constant(100)
vector = tf.constant([1,2,3,4,5])
matrix = tf.constant([[1,2,3],[4,5,6]])
cube_matrix = tf.constant([[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]])
print(scalar)
print(vector)
print(matrix)
print(cube_matrix)
Tensor("Const:0", shape=(), dtype=int32)
Tensor("Const_1:0", shape=(5,), dtype=int32)
Tensor("Const_2:0", shape=(2, 3), dtype=int32)
Tensor("Const_3:0", shape=(3, 3, 1), dtype=int32)
#形状
scalar.get_shape()
TensorShape([])
vector.get_shape()
TensorShape([Dimension(5)])
matrix.get_shape()
cube_matrix.get_shape()
TensorShape([Dimension(3), Dimension(3), Dimension(1)])
#数据类型
import tensorflow as tf
import numpy as np
tensor_1d = np.array([1,2,3,4,5,6,7,8,9,10])
tensor_1d = tf.constant(tensor_1d)
with tf.Session() as sess:
    print(tensor_1d.get_shape())
    print(sess.run(tensor_1d))
(10,)
[ 1  2  3  4  5  6  7  8  9 10]
#InteractiveSession
import tensorflow as tf
import numpy as np
interactiveSession = tf.InteractiveSession()
tensor = np.array([1,2,3,4,5])
tensor = tf.constant(tensor)
print(tensor.eval())
interactiveSession.close()
[1 2 3 4 5]
#转为tensor
import tensorflow as tf
import numpy as np
tensor_3d = np.array([[[1],[2],[3]],[[1],[2],[3]],[[1],[2],[3]]])
tensor_3d = tf.convert_to_tensor(tensor_3d,dtype=tf.float64)
with tf.Session() as sess:
    print(tensor_3d.get_shape())
    print(sess.run(tensor_3d))
(3, 3, 1)
[[[1.]
  [2.]
  [3.]]

 [[1.]
  [2.]
  [3.]]

 [[1.]
  [2.]
  [3.]]]
import tensorflow as tf
value = tf.Variable(0,name="value")
one = tf.constant(10)
new_value = tf.add(value,one)
update_value = tf.assign(value,new_value)
initialize_var = tf.global_variables_initializer()
with tf.Session() as sess:
    #初始化变量
    sess.run(initialize_var)
    print(sess.run(value))
    #更新值
    for _ in range(10):
        sess.run(update_value)
        print(sess.run(value))
0
10
20
30
40
50
60
70
80
90
100
import tensorflow as tf

constant_A = tf.constant([100.0])
constant_B = tf.constant([300.0])
constant_C = tf.constant([3.0])

sum_ = tf.add(constant_A,constant_B)
mul_ = tf.multiply(constant_A,constant_C)
with tf.Session() as sess:
    result = sess.run([sum_,mul_])
    print(result)
[array([400.], dtype=float32), array([300.], dtype=float32)]
#注入机制:先建立占位符,然后望里面输入值
import tensorflow as tf
import numpy as np
a = 3
b = 2
x = tf.placeholder(tf.float32,shape=(a,b))
y = tf.add(x,x)
data = np.random.rand(a,b)
sess = tf.Session()
print(sess.run(y,feed_dict={x:data}))
[[0.51678205 1.8697306 ]
 [1.3825455  0.98651147]
 [0.7895504  0.32539564]]

实现一个单输入神经元

import tensorflow as tf
input_value = tf.constant(0.5,name="input_value")
weight = tf.Variable(1.0,name="weight")
expected_output = tf.constant(0.0,name="expected_output")
model = tf.multiply(input_value,weight,"model")
loss_function = tf.pow(expected_output-model,2,name = "loss_function")
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.025).minimize(loss_function)

for value in [input_value,weight,expected_output,model,loss_function]:
    tf.summary.scalar(value.op.name,value)
summaries = tf.summary.merge_all()
sess = tf.Session()
summary_writer = tf.summary.FileWriter('log_simple_stats',sess.graph)
sess.run(tf.global_variables_initializer())
for i in range(100):
    summary_writer.add_summary(sess.run(summaries),i)
    sess.run(optimizer)

总结

Tensorflow-gpu的安装是一件及其麻烦的事情。在ubuntu上首先安装NVIDIA显卡驱动,然后安装cuda9.0+cudnn7.0,然后测试是否安装成功,然后安装anaconda,安装tensorflow-gpu1.8.
tensorflow采用计算图实现模型搭建。首先定义常量,变量,占位符等,然后生成计算图,并优化计算,使其高效运行。其中Variable必须指定初值,而Placeholder不必指定初值,可以在sess.run时指定feed_dict。variable放训练变量,而placeholder传入值。tensorflow的数据使用张量来表示(多维向量),可以通过get_shape获取形状,如(5,)代表一维数组。运行时首先定义好计算图,然后通过session启动计算,可以指定计算一个变量,或者多个变量。
Tensorboard可视化显示运算过程。

相关文章

网友评论

      本文标题:Deep Learning with Tensorlfow:ch

      本文链接:https://www.haomeiwen.com/subject/riqqyqtx.html