tensorflow 学习纪录(持续更新)
1 import tensorflow as tf 2 import numpy as np 3 4 #tensor = tf.constant([[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8]]) 5 tensor = tf.placeholder(tf.int32, [2,8]) 6 7 with tf.Session() as sess: 8 sess.run(tf.global_variables_initializer()) 9 print sess.run(tensor,feed_dict={tensor:[[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8]]}) 10 print sess.run(tensor) 11 tensorReshape = tf.reshape(tensor,[-1,4]) 12 print sess.run(tensorReshape)
print sess.run(tensor) 会报错,
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype int32 and shape [2,8]
[[Node: Placeholder = Placeholder[dtype=DT_INT32, shape=[2,8], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
原因:feed 使用一个 tensor 值临时替换一个操作的输出结果. 你可以提供 feed 数据作为 run()
调用的参数. feed 只在调用它的方法内有效, 方法结束, feed 就会消失。
tensorflow 加减op实验结果
y_out = tf.constant([1.0,2.0,3.0],shape=[3]) #y_out = tf.constant([[1.0,2.0,3.0],[2.0,3.0,4.0]],shape=[2,3]) yy = tf.constant([[1.0,2.0,3.0],[4.0,5.0,6.0],[1.0,2.0,3.0]],shape=[3,3]) print sess.run(y_out+yy)
二维和三维矩阵相乘
tf.matmul(M1,M2)
三维*二维
M1 = tf.Variable(tf.random_normal([2,3,4])
M2 = tf.Variable(tf.random_normal([5,4])
N = tf.einsum('ijk,lk->ijl',M1,M2)
tensorflow attention
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation 'Adadelta/update_post_embedding/Cast': Could not satisfy explicit device specification '' because the node was colocated with a group of nodes that required incompatible device '/job:localhost/replica:0/task:0/device:GPU:0'
安装完tensorflow-gpu版本后,跑程序会报这样的错,是因为GPU不支持一些op运算。
解决办法是安装对应的tensorflow cpu版本就完美解决了。
参考:https://github.com/cysmith/neural-style-tf/issues/18
tensorflow-gpu 1.4.1 不支持这两个函数
#optimizer = tf.train.AdadeltaOptimizer(learning_rate=1)
#optimizer = tf.train.AdagradOptimizer(learning_rate=0.1)