【发布时间】:2017-02-25 07:01:38
【问题描述】:
我有二进制输出 - '1' 或 '0'。
我没有将它编码为 one-hot(因为我最初认为没有任何目的),当我运行我的 conv 网络模型时,我得到了奇怪的结果 - 所有输出都是 '1' 并且准确度是 ~57 %。
我感觉有些不对劲。
所以我的问题是:我们是否总是需要将标签编码为 one-hot?如果是这样,为什么(在二进制的情况下)?
我的代码:
{'class': tf.argmax(prediction, 1) 行建议应该有多个输出(如向量),然后我们以最大概率取向量中的一个元素 -- 这种解释正确吗? 所以它得到了我想我可能应该为二进制输出输出 2 个标签......
另外,我试图在行中输出实际概率
return {'class': prediction, 'prob': prediction}, loss, train_op
但它似乎没有用,我最终得到的只是 [1 1 1 ...1]
我的转化模型:
def my_conv_model(x, y):
# 1. form a 4d tensor of shape N x 1 x N_FEATURES x 1
x = tf.reshape(x, [-1, 1, N_FEATURES, 1])
##########################################################################
##### Conv layer 1 #####
conv1 = tf.contrib.layers.convolution2d(inputs=x,
num_outputs=N_FILTERS,
kernel_size=[1, 7],
stride=[1, 1],
padding='VALID')
# 3. Add a RELU for non linearity.
conv1 = tf.nn.relu(conv1)
# 4. Max pooling across output of Convolution+Relu.
pool1 = tf.nn.max_pool(conv1,
ksize=[1, 1, 3, 1],
strides=[1, 1, 3, 1],
padding='SAME')
##########################################################################
##### Conv layer 2 #####
conv2 = tf.contrib.layers.convolution2d(inputs=pool1,
num_outputs=N_FILTERS,
kernel_size=[1, 7],
padding='VALID')
pool2 = tf.nn.max_pool(conv2,
ksize=[1, 1, 2, 1],
strides=[1, 1, 2, 1],
padding='SAME')
last_pool_layer = pool2
last_pool_layer_shape = last_pool_layer.get_shape()
n_cols = (last_pool_layer_shape[2] * last_pool_layer_shape[3]).value
last_pool_layer = tf.reshape(last_pool_layer, [-1, n_cols])
fc_layer = tf.contrib.layers.fully_connected(inputs=pool2,
num_outputs=10,
activation_fn=tf.nn.relu)
last_layer = fc_layer
try:
last_layer_shape = last_layer.get_shape()
print("last_layer_shape", last_layer_shape)
last_layer = tf.reshape(last_layer, [-1, (last_layer_shape[2] * last_layer_shape[3]).value])
print("last_layer_shape", last_layer.get_shape())
exc_info = sys.exc_info()
y = tf.expand_dims(y, 1)
prediction, loss = learn.models.logistic_regression(last_layer, y)
print("prediction", prediction)
prediction = tf.Print(prediction, [prediction], message="This is a: ")
#print(prediction.eval())
train_op = tf.contrib.layers.optimize_loss(
loss=loss,
global_step=tf.contrib.framework.get_global_step(),
optimizer='SGD',
learning_rate=0.001)
#return {'class': tf.argmax(prediction, 1), 'prob': prediction}, loss, train_op
return {'class': prediction, 'prob': prediction}, loss, train_op
【问题讨论】:
标签: machine-learning tensorflow deep-learning prediction conv-neural-network