tensorflow实验记录二
这才第二天,扶朕起来朕还能写
2020.02.17
1.中间变量的定义类型
当前倾向于用constant张量与numpy结合定义,但实践觉得不太行
2.张量的具体数值提取
参考https: // blog.youkuaiyun.com / chr1991 / article / details / 93474638
由上篇试验得知不能用a[i]来提取,今天试验另一种方法,代码如下:
ref = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
indices = tf.constant([[4], [3], [1] ,[7]])
updates = tf.constant([9, 10, 11, 12])
update = tf.scatter_nd_update(ref, indices, updates)
with tf.Session() as sess:
print sess.run(update)
啊它好半天没反应,显示了这个:
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable
不行啧 :<,后续发现是因为这个憨憨没初始化变量,初始化后能不能用不晓得懒得试啦
但是零向量赋值的这个就莫得问题
indices = tf.constant([[4], [3], [1], [7]])
updates = tf.constant([9, 10, 11, 12])
shape = tf.constant([8])
scatter = tf.scatter_nd(indices, updates, shape)
with tf.Session() as sess:
print(sess.run(scatter))
结果正常
这就,很有些麻烦
思考一下,还是能矩阵运算就矩阵运算吧
3.来个常见矩阵运算测试表示今天划水的不是特别厉害。。。
测试前置:
aa=tf.constant([2.0,5.0]);
bb=tf.constant([3.0,8.0]);
cc=tf.constant([-5.0,-4.0]);
aav=tf.Variable([2.0,5.0]);
bbv=tf.Variable([3.0,8.0]);
ccv=tf.Variable([-5.0,-4.0]);
with tf.Session() as sess:
print(sess.run(cc))
- tf.asign(x,y,name=None) 效果为令x=y
cc=tf.assign(aa,dd,name=None);
cc = tf.assign(dd, aa, name=None);
dd = tf.assign(aa, dd, name=None);
结果第一个就报错啦,assign只能用在Variable上(这个对目前思路来讲就莫得用),变量shape要一样,更改代码如下:
ccv = tf.assign(aav, dd, name=None);
这种情况下,ccv打印出来仍旧是原值,aav就会报错 Attempting to use uninitialized value Variable [[{{node _retval_Variable_0_0}}]],即未初始化,具体的内容能看出与bbv的区别在于variable的不同
aav: <tf.Variable ‘Variable:0’ shape=(2,)dtype=float32_ref>
bbv: <tf.Variable ‘Variable_1:0’ shape=(2,)dtype=float32_ref>
所以说果然和numpy库之间有兼容性的问题…
这么一试感觉还是应该先去细看看官方文档和API,先放个tbc在这里
2020.02.17
昨天感觉没写啥,今天接着写吧,事实证明还是应该多看文档,不能一!目!十!行!变量还是应该用variable搞,敲脑壳
2020.02.18
4.必要函数
- 数据类型转换
t2 = tf.cast(t1,dtype=tf.float32)
numpy转换函数则是a.astype(np.complex)
- tf.map_fn
似乎可以用来将张量中每个元素带入函数
参考https://mp.weixin.qq.com/ssrc=11×tamp=1582265847&ver=2171&signature=SuwSZ0T793xX6K0j1uFnRZCNh0Otia2tmmNlq-Wo72mtSBoeLFqdKA5WYV3XyUaFLdj68u4jo6vzbwDcaJ48SornJn0TXxUUn2irqV2YyFlMfnzvyagbwytdVfEI2w&new=1
map_fn(fn, #一个可调用函数,用来对elems中的元素进行处理
elems, #要处理的tensor,tf会从第0维(最外层)展开,进行map操作,如一批图片[batchsiz,H,W,C],则是对其中的每一张图片调用函数fn进行处理
dtype=None, # fn函数的返回类型(对elems处理后的返回类型),如果fn返回的类型和输入elems中的不通,此时需要显式指定fn的返回类型
parallel_iterations=None, # 允许并行运行的迭代次数
back_prop=True,
swap_memory=False,
infer_shape=True, #对一致输出形状的测试,建议选择False
name=None)#返回的张量的名称前缀
测试代码如下:
def squareElements(arg):
res = arg * arg
res = tf.cast(res,dtype=tf.float32)
return res*1.0
squares = tf.map_fn(squareElements, aa, dtype=tf.float32)
输出正常,看着可行
把aa换成variable就有点问题,卡在这步很久没结果
2020-02-21 14:33:03.490993: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
两个结果显示结构都是tensor,应当是无法处理variable
另外试一下递归
roundss=tf.map_fn(squareElements, squares, dtype=tf.float32)
可行
行吧这一步可以这代码差不多就能写了
-
tf.reshape
可用于改变输入的维度情况,感觉一维更好算一点,需注意并不能改变元素个数 -
np.reshape与np.resize
reshape与上同,resize可改变维度,若大删除,若小填充。 -
tf.transpose
矩阵转置 -
tf.complex(real,img)
实现复数生成,因为发现直接*1j跑不了,乘序列1j显示
can’t multiply sequence by non-int of type ‘RefVariable’,狒狒挠头
本函数用于变量常量均可
4.Variable的尝试下标调用
张量啦告辞,我真实善变
6.Variable初始化问题
参考https://blog.youkuaiyun.com/bestrivern/article/details/88124329
#声明一个依赖于其他变量的变量
# W is a random 700 x 100 tensor
W = tf.Variable(tf.truncated_normal([700, 10]))
U = tf.Variable(W * 2)
U = tf.Variable(W.intialized_value() * 2)#确保W在给U赋值前已经初始化完毕
7.所有的Variable都会被优化器当作需要训练的量来扔到训练里吗
加上trainable=False,就可以啦
8.神经网络结构是否可以直接写循环构造重复层
先跑通再说
9.矩阵乘法问题
- 点乘
A*B,等价于tf.multiply(A,B)
两种情况,维度一致对应相乘,或前一项shape为[N,1],后一项为[N,M] - 数乘
正常矩阵乘法,tf.matmul(A,B)
9.placeholder读入的数据和设定占据的数据格式不一样会怎么样