#coding=utf-8import tensorflow as tf
import numpy as np
defreadMyFileFormat(fileNameQueue):
reader = tf.TextLineReader()
key, value = reader.read(fileNameQueue)
record_defaults = [[1], [1], [1]]
col1, col2, col3 = tf.decode_csv(value, record_defaults = record_defaults)
features = tf.pack([col1, col2])
label = col3
return features, label
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
其中,record_defaults = [[1], [1], [1]] ,是用于指定矩阵格式以及数据类型的,CSV文件中的矩阵,是NXM的,则此处为1XM,[1]中的1 用于指定数据类型,比如矩阵中如果有小数,则为float,[1]应该变为[1.0]。
col1, col2, col3 = tf.decode_csv(value, record_defaults = record_defaults) , 矩阵中有几列,这里就要写几个参数,比如5列,就要写到col5,不管你到底用多少。否则报错。
tf.unstack与tf.stack
原型:
unstack(
value,
num=None,
axis=0,
name='unstack' )
官方解释:https://tensorflow.google.cn/api_docs/python/tf/unstack
解释:这是一个对矩阵进行分解的函数,以下为关键参数解释:
value:代表需要分解的矩阵变量(其实就是一个多维数组,一般为二维);
axis:指明对矩阵的哪个维度进行分解。
要理解tf.unstack函数,我们不妨先来看看tf.stack函数。Tf.stack刚好是与tf.unstack函数相反,前者是对矩阵进行拼接,后者则对矩阵进行分解。
转载出处:
https://www.jianshu.com/p/25706575f8d4
数组的行列要分开,矩阵要画成np.array
import numpy as np
x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]])
输出数组的行和列数
print x.shape # (4, 3)
只输出行数
print x.shape[0] # 4
只输出列数
print x.shape[1] # 3
图像通道转换[n c h w] 转 [n h w c]
Tensorflow定义的tensor的shape为[n,h,w,c],而我们直接读取文件格式是[n,c,h,w],为了转成[n,h,w,c]形势,可以采用三种方法:
img = np.transpose(img, (0, 2, 3, 1))
img = img.reshape(img.shape[0], img.shape[2], img.shape[3], img.shape[1])
img_new = zeros((img.shape[0], img.shape[2], img.shape[3], img.shape[1]), dtype = np.float32)
for c in range(0, img.shape[1]):
for i in range(0, img.shape[2]):
for j in range(0, img.shape[3]):
img_new[:,c,i,j] = img_new[:,i,j,c]