我有二進制輸出 - '1'或'0'。是否有必要將標籤編碼爲tersorflow中的一個熱點?
我不會把它編碼爲一熱(因爲我最初認爲沒有目的),當我運行我的網絡模型時,我得到了奇怪的結果 - 所有輸出都是'1',精度是〜 57%。
我覺得有什麼不對。
所以我的問題是:我們是否總是需要將標籤編碼爲一熱?如果是這樣,爲什麼(在二進制的情況下)?
我的代碼:
線{'class': tf.argmax(prediction, 1)
建議,應該有多種輸出(如矢量),然後我們把一個元素與最大概率的載體 - 是這種理解是否正確?所以這讓我想,我也許應該輸出2個標籤二進制輸出...
而且,我試圖輸出的實際概率在該行
return {'class': prediction, 'prob': prediction}, loss, train_op
,但似乎並沒有工作,和所有我得到最終爲[1 1 1 ... 1]
我CONV型號:
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