Python 如何在“tensorflow”中修正精度函数?
我在评估正确的预测时出错 这是我的全部代码:Python 如何在“tensorflow”中修正精度函数?,python,machine-learning,tensorflow,conv-neural-network,Python,Machine Learning,Tensorflow,Conv Neural Network,我在评估正确的预测时出错 这是我的全部代码: def compute_accuracy(testimgName , testimgLabel ): global prediction v_xs = np.empty([1,224,224], dtype=np.int) v_ys = np.empty([1,8], dtype=np.float32) g = testimgName[1] v_xs = np.array(Image.open(path+g)
def compute_accuracy(testimgName , testimgLabel ):
global prediction
v_xs = np.empty([1,224,224], dtype=np.int)
v_ys = np.empty([1,8], dtype=np.float32)
g = testimgName[1]
v_xs = np.array(Image.open(path+g))
v_xs = np.reshape(v_xs,[1,224,224])
v_ys = testimgLabel[1] ## It is a list = ['0', '0', '0', '0', '0', '0', '1', '0']
v_ys = np.reshape(v_ys , [1,8])##print(v_ys) = [['0' '0' '0' '0' '0' '0' '1' '0']]
y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1})
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
return result
def weight_variable(shape):
initial = tf.truncated_normal(shape , stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1,shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
#stride [1,x_movment,y_movment,1]
#Must have strides[0] = strides[3] = 1
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME') #stride [batch,height,width,channels]
def max_pool_2x2(x):
#stride[1,x_movment,y_movment ,1]
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1], padding='SAME')
# size of image
image_height = 224
image_width = 224
number_of_class = 8
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 224, 224]) # 224*224
ys = tf.placeholder(tf.float32, [None, 8])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs,[-1,224,224,1])
## conv1 layer ##
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+ b_conv1) #output size 224*224*32
h_pool1 = max_pool_2x2(h_conv1) # 112*112*32
## conv2 layer ##
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+ b_conv2) #output size 112*112*64
h_pool2 = max_pool_2x2(h_conv2) # output size of pooling 56*56*64
## conv3 layer ##
W_conv3 = weight_variable([5,5,64,64])
b_conv3 = bias_variable([64])
h_conv3 = tf.nn.relu(conv2d(h_pool2,W_conv3)+ b_conv3) #output size 56*56*64
h_pool3 = max_pool_2x2(h_conv3) # output size of pooling 28*28*64
## conv4 layer ##
W_conv4 = weight_variable([5,5,64,32])
b_conv4 = bias_variable([32])
h_conv4 = tf.nn.relu(conv2d(h_pool3,W_conv4)+ b_conv4) #output size 28*28*32
h_pool4 = max_pool_2x2(h_conv4) # output size of pooling 14*14*32
## func1 layer ##
W_fc1 = weight_variable([14*14*32,256])
b_fc1 = bias_variable([256])
#[n_sample, 14,14,32] ->> [n_sample,14*14*32]
h_pool4_flat = tf.reshape(h_pool4,[-1,14*14*32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool4_flat,W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
## func2 layer ##
W_fc2 = weight_variable([256,8])
b_fc2 = bias_variable([8])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)
# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
reduction_indices=[1])) # loss
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
trainimgNames , trainimgLabels = ut.getlabel(path,textFileName_train)
imgTestNames , imgTestLabels = ut.getlabel(path,textFileName_test)
batch_size = 1
number_of_batches = len(trainimgNames)/batch_size
for i in range(int(number_of_batches)):
batch_x = trainimgNames[i*batch_size:i*batch_size+batch_size]
batch_y = trainimgLabels[i*batch_size:i*batch_size+batch_size]
batch_image_data = np.empty([batch_size,image_height,image_width], dtype=np.int)
for j in range(len(batch_x)):
f = batch_x[j]
batch_image_data[j] = np.array(Image.open(path+f))
sess.run(train_step, feed_dict={xs: batch_image_data, ys:batch_y, keep_prob: 0.5})
print(compute_accuracy(imgTestName , imgTestLabel))
而getlabel是:
当我调用printcomputer_Accurance时,会出现错误
错误是
我不知道为什么y_pre会这样,预测会出错。有人能帮我修复它吗?TensorFlow的哲学与纯Python不同。首先定义张量,然后运行它 在第一部分中,您定义了预测,但没有运行任何内容。它是声明性的。您声明预测取决于不同的变量/操作 仅当您调用sess.run时才会进行计算。因此,y_pres不是张量,它是一个python变量numpy,可能包含结果。因此,不应该将其传递给TF函数 那又怎样?因此,您应该将Tensorflow声明性部分与运行部分分开。这意味着,只保留第二个sess.run调用,此调用和第一个sess.run之间的所有内容都属于声明性部分。它应该看起来有点像: 免责声明:下面的代码甚至不起作用,请参见问题1或其他问题 定义张量的声明性部分,即TF的计算图:
您的代码不完整。您的代码段中未定义预测。请提供一个完整的和可复制的例子。是的,你是对的,我应该写它@所以下面的答案是正确的;您应该将定义Tensorflow图的代码与运行它的代码分开。至于您的具体问题,似乎计算精度中的一个或多个numpy数组或张量是string类型的。如果我读对了代码,那么标签值就是字符串。你应该把它们转换成数字。请看mnist_softmax.py示例:。希望有帮助!我刚刚编辑了我的代码。这是全部代码。你能再看看吗@PLTRDY然后您应该应用我建议的更改,即删除1个sess.run。您在sess.run返回的numpy对象上调用TF函数这一事实一定会引起麻烦。@amirMaleki情况如何?正如您所说的@pltrdy我更改了代码,但它工作不正确,我正在尝试修复发生的语义错误。我很感激你
def getlabel(path,txtFileName):
names = []
labels = []
dirs = os.listdir( path )
with open(txtFileName, 'r') as f:
for line in f.readlines():
tokens = line.split(' ')
names.append(tokens[0])
labels.append(int(tokens[1]))
i = 0
listLabel= []
f = []
for item in labels:
a = ('{0:08b}'.format(labels[i]))
listLabel.append(list(a))
i+=1
return names,listLabel
TypeError: DataType string for attr 'T' not in list of allowed values: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, float16
v_ys = tf.Placeholder( <type> )
W_fc1 = weight_variable([14*14*32,256])
b_fc1 = bias_variable([256])
h_pool4_flat = tf.reshape(h_pool4,[-1,14*14*32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool4_flat,W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
W_fc2 = weight_variable([256,8])
b_fc2 = bias_variable([8])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)
# Defining correct prediction as a Tensor as well
correct_prediction = tf.equal(tf.argmax(prediction,1), tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
def compute_accuracy(imageTestName , imageTestLabel ):
global prediction
v_xs = np.empty([1,224,224], dtype=np.int)
g = imageTestName[0]
v_xs = np.array(Image.open(path+g))
v_xs = np.reshape(v_xs,[1,224,224])
v_ys = imageTestLabel[0] ## It is a list = ['0', '0', '0', '0', '0', '0', '1', '0']
v_ys = np.reshape(v_ys , [1,8]) ## After reshape it is = [['0' '0' '0' '0' '0' '0' '1' '0']]
# Directly evaluate `accuracy`
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
return result