我最后添加了一个'print'语句,但是它没有得到正确的输出。
import xlrd
import numpy as np
import tensorflow as tf
data = xlrd.open_workbook('ML_data1.xlsx') #读excel文档
table = data.sheet_by_name('Sheet2') #读文档里的一个表格数据
table1 = data.sheet_by_name('Sheet3')
train_data = np.zeros((452, 279), dtype=float) #建立一个元素全为0的数组
train_data = np.float32(train_data)
train_label = np.zeros((452, 16), dtype=float)
train_label = np.float32(train_label)
for i in range(452):
for j in range(279):
train_data[i, j] = table.cell(i, j).value #将表格数据存入数组
for a in range(452):
for b in range(1, 17):
if(table1.cell(a+1, 0).value == b):
train_label[a, b-1] = 1
x = tf.placeholder("float", [None, 279])
y = tf.placeholder("float", [None, 16])
def add_layer(inputs, input_node, output_node, active_function=None):
w1 = tf.Variable(tf.random_normal([input_node, output_node]))
b1 = tf.Variable(tf.zeros([1.0, output_node])+0.1)
y1 = tf.matmul(inputs, w1) + b1
if(active_function==None):
outputs = y1
else:
outputs = active_function(y1)
return outputs
y2 = add_layer(x, 279, 200, active_function=tf.nn.relu)
y3 = add_layer(y2, 200, 100, active_function=tf.nn.relu)
y4 = add_layer(y3, 100, 16, active_function=tf.nn.relu)
loss = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=y4,labels=y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
corr = tf.equal(tf.argmax(y4, 1), tf.argmax(y, 1))
accr = tf.reduce_mean(tf.cast(corr, "float"))
init = tf.global_variables_initializer()
sess= tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train_step, feed_dict={x: train_data, y: train_label})
if(i % 50 == 0):
a = sess.run(loss, feed_dict={x: train_data, y: train_label})
print("loss: %.3f" % a)
这是一个简单的BP网络,用于对16个类别进行分类。数据集是542x279的矩阵,这意味着有542个样本,每个样本具有279个属性。标签为542x16矩阵,每行只有一个“ 1”,另一个为“ 0”,这表示准确的分类。
答案 0 :(得分:0)
它将永远不会打印。查看我添加的评论。
for i in range(1000):
sess.run(train_step, feed_dict={x: train_data, y: train_label})
# here, i = 999 : range(1000) goes from 0 to 999
if(i % 50 == 0):
# 999 % 50 != 0, so no print!
a = sess.run(loss, feed_dict={x: train_data, y: train_label})
print("loss: %.3f" % a)
我认为您打算缩进最后一个块(for循环的一部分)。按照书面规定,它将永远不会执行。