我是tensorflow的初学者,我使用 tf.expand_dims ,我得到错误,我无法理解原因,所以我错过了什么?
这是代码
ML_OUTPUT = None
input_for_classification = None
def ConstructML( input_tensor, layers_count, node_for_each_layer):
global ML_OUTPUT
global input_for_classification
FeatureVector = np.array(input_tensor)
FeatureVector = FeatureVector.flatten()
print(FeatureVector.shape)
ML_ModelINT(FeatureVector, layers_count, node_for_each_layer)
def ML_ModelINT(FeatureVector, layers_count, node_for_each_layer):
hidden_layer = []
Alloutputs = []
hidden_layer.append({'weights': tf.Variable(tf.random_normal([FeatureVector.shape[0], node_for_each_layer[0]])),'biases': tf.Variable(tf.random_normal([node_for_each_layer[0]]))})
for i in range(1, layers_count):
hidden_layer.append({'weights': tf.Variable(tf.random_normal([node_for_each_layer[i - 1], node_for_each_layer[i]])),'biases': tf.Variable(tf.random_normal([node_for_each_layer[i]]))})
FeatureVector = tf.expand_dims(FeatureVector,0)
layers_output = tf.add(tf.matmul(FeatureVector, hidden_layer[0]['weights']), hidden_layer[0]['biases'])
layers_output = tf.nn.relu(layers_output)
Alloutputs.append(layers_output)
for j in range(1, layers_count):
layers_output = tf.add(tf.matmul(layers_output, hidden_layer[j]['weights']), hidden_layer[j]['biases'])
layers_output = tf.nn.relu(layers_output)
Alloutputs.append(layers_output)
ML_OUTPUT = layers_output
input_for_classification = Alloutputs[1]
return ML_OUTPUT
ML_Net = ConstructML(input,3,[1024,512,256])
它在这一行中给我错误
FeatureVector = tf.expand_dims(FeatureVector,0)
错误是预期的二进制或unicode字符串,得到tf.Tensor'Relu_11:0'形状=(?,7,7,512)dtype = float32
注意输入是另一个网络的输出张量,它运行良好
答案 0 :(得分:1)
Okey, numpy 部分是错误的,因为当首次调用预定函数时,它还没有用于input_imgs的提要,并且numpy代码将无法正确执行,并且我用tensorflow操作代替它,现在就可以了。