【发布时间】:2016-07-12 05:58:09
【问题描述】:
有一个简单且具有教育意义的玩具分类器(2 个全连接层)作为 JAVA 小程序:http://cs.stanford.edu/people/karpathy/convnetjs/demo/classify2d.html
这里,输入是带有 {0,1} 标签的二维点列表。如您所见,他们将架构定义如下。
layer_defs = [];
layer_defs.push({type:'input', out_sx:1, out_sy:1, out_depth:2});
layer_defs.push({type:'fc', num_neurons:6, activation: 'tanh'});
layer_defs.push({type:'fc', num_neurons:2, activation: 'tanh'});
layer_defs.push({type:'softmax', num_classes:2});
我正在尝试使用 tensorflow 进行测试,如下所示。
pts = tf.placeholder(tf.float32, [None,2], name="p")
label = tf.placeholder(tf.int32, [None], name="labels")
with tf.variable_scope("layers") as scope:
fc1 = fc_layer(pts, [2, 6], "fc1")
fc1 = tf.nn.tanh(fc1)
fc2 = fc_layer(fc1, [6, 2], "fc2")
fc2 = tf.nn.tanh(fc2)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(fc2, label, name='cross_entropy_per_example')
cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)
train_op = optimizer.minimize(cross_entropy_mean, global_step=global_step)
而函数fc_layer不过是
def fc_layer(bottom, weight_shape, name):
W = tf.get_variable(name+'W', shape=weight_shape, dtype=tf.float32, initializer=tf.random_normal_initializer(mean = 0.01,stddev=0.01))
b = tf.get_variable(name+'b', shape=[weight_shape[1]], dtype=tf.float32, initializer=tf.random_normal_initializer(mean = 0.01,stddev=0.01))
fc = tf.nn.bias_add(tf.matmul(bottom, W), b)
return fc
但是,损失似乎并没有减少。损失定义(交叉熵)有问题吗?
谁能帮忙?
【问题讨论】:
-
您似乎批量输入输入,但
label仅排名 1。你的模型有标签吗? -
是的。例如
P=[[-0.2443358, 0.04343621], [-0.45233325, 0.27792488], [0.21115686, -0.02241944]], 和L=[1, 1, 0],它们被馈送到@98765和label -
我现在明白了,抱歉:我不知道这个表格是
tf.nn.sparse_softmax_cross_entropy_with_logits的要求,如documentation 中所述。
标签: python tensorflow