首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorFlow sparse_softmax_cross_entropy秩误差

TensorFlow sparse_softmax_cross_entropy秩误差
EN

Stack Overflow用户
提问于 2017-11-03 21:49:07
回答 1查看 1.6K关注 0票数 1

我正试图在TensorFlow上用LSTM构建一个RNN。输入和输出都是5000×2矩阵,其中列表示特征。然后,这些矩阵被输入到batchX和batchY占位符,这些占位符支持反向传播。代码的主要定义在底部。我得到了以下错误:

等级错配:标签的等级(接收到2)应该等于逻辑的-1(接收到2)。

我检查了logits_serieslabels_series,它们似乎都包含[batch_size, num_features]形状的张量的反向传播量。

我感到困惑的是:既然逻辑是标签的预测,它们不应该有相同的维度吗?

代码语言:javascript
复制
'''
RNN definitions

input_dimensions = [batch_size, truncated_backprop_length, num_features_input] 
output_dimensions = [batch_size, truncated_backprop_length, num_features_output]
state_dimensions = [batch_size, state_size]
'''
batchX_placeholder = tf.placeholder(tf.float32, (batch_size, truncated_backprop_length, num_features_input))
batchY_placeholder = tf.placeholder(tf.int32, (batch_size, truncated_backprop_length, num_features_output))
init_state = tf.placeholder(tf.float32, (batch_size, state_size))
inputs_series = tf.unstack(batchX_placeholder, axis=1)
labels_series = tf.unstack(batchY_placeholder, axis=1)

w = tf.Variable(np.random.rand(num_features_input+state_size,state_size), dtype = tf.float32)
b = tf.Variable(np.zeros((batch_size, state_size)), dtype = tf.float32)
w2 = tf.Variable(np.random.rand(state_size, num_features_output), dtype = tf.float32)
b2 = tf.Variable(np.zeros((batch_size, num_features_output)), dtype=tf.float32)

#calculate state and output variables

state_series = []
output_series = []
current_state = init_state
#iterate over each truncated_backprop_length
for current_input in inputs_series:
    current_input = tf.reshape(current_input,[batch_size, num_features_input])
    input_and_state_concatenated = tf.concat([current_input,current_state], 1)
    next_state = tf.tanh(tf.matmul(input_and_state_concatenated, w) + b)
    state_series.append(next_state)
    current_state = next_state
    output = tf.matmul(current_state, w2)+b2
    output_series.append(output)

#calculate expected output for each state    
logits_series = [tf.matmul(state, w2) + b2 for state in state_series] 
#print(logits_series)
predictions_series = [tf.nn.softmax(logits) for logits in logits_series]
'''
batchY_placeholder = np.zeros((batch_size,truncated_backprop_length))
for i in range(batch_size):
    for j in range(truncated_backprop_length):
        batchY_placeholder[i,j] = batchY1_placeholder[j, i, 0]+batchY1_placeholder[j, i, 1]
'''
print("logits_series", logits_series)
print("labels_series", labels_series)
#calculate losses given each actual and calculated output
losses = [tf.nn.sparse_softmax_cross_entropy_with_logits(logits = logits, labels = labels) for logits, labels in zip(logits_series,labels_series)]
total_loss = tf.reduce_mean(losses)
EN

回答 1

Stack Overflow用户

发布于 2017-11-06 20:20:12

多亏了陈茂熙,我发现了这个问题。那是因为

tf.nn.sparse_softmax_cross_entropy_with_logits

要求标签比逻辑少一个维度。具体来说,标签参数接受形状[batch_size] and the dtype int32 or int64的值。

我通过枚举我拥有的一个热编码标签来解决这个问题,减少了维度。

然而,它也有可能使用

tf.nn.softmax_cross_entropy_with_logits

,它没有降维要求,因为它使用带有[batch_size, num_classes] and dtype float32 or float64.形状的标签值。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47104863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档