简体   繁体   English

Tensorflow 嵌入层后跟 Dense 会导致形状错误

[英]Tensorflow Embedding layer followed by Dense gives shape error

Actually, I'm trying to convert this old Tensorflow 1 code to TF2 using keras:实际上,我正在尝试使用 keras 将这个旧的 Tensorflow 1 代码转换为 TF2:

def init_net(batch_size=256, num_feats=30, hidden_size=100):
  with tf.name_scope('network'):
    with tf.name_scope('inputs'):
        
        inputs = tf.placeholder(tf.int32, shape=[batch_size, ], name='inputs')
        labels = tf.placeholder(tf.int32, shape=[batch_size, ], name='labels')

        embeddings = tf.Variable(
            tf.random_uniform([len(NODE_MAP), num_feats]), name='embeddings'
        )

        embed = tf.nn.embedding_lookup(embeddings, inputs)
        onehot_labels = tf.one_hot(labels, len(NODE_MAP), dtype=tf.float32)

    with tf.name_scope('hidden'):
        weights = tf.Variable(
            tf.truncated_normal(
                [num_feats, hidden_size], stddev=1.0 / math.sqrt(num_feats)
            ),
            name='weights'
        )

        biases = tf.Variable(
            tf.zeros((hidden_size,)),
            name='biases'
        )

        hidden = tf.tanh(tf.matmul(embed, weights) + biases)

    with tf.name_scope('softmax'):
        weights = tf.Variable(
            tf.truncated_normal(
                [hidden_size, len(NODE_MAP)],
                stddev=1.0 / math.sqrt(hidden_size)
            ),
            name='weights'
        )
        biases = tf.Variable(
            tf.zeros((len(NODE_MAP),), name='biases')
        )

        logits = tf.matmul(hidden, weights) + biases

    with tf.name_scope('error'):
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
            labels=onehot_labels, logits=logits, name='cross_entropy'
        )

        loss = tf.reduce_mean(cross_entropy, name='cross_entropy_mean')

return inputs, labels, embeddings, loss

NODE_MAP here is the vocabulary. NODE_MAP 这里是词汇表。 This network should be able to learn a programming language.该网络应该能够学习编程语言。 My version is the following:我的版本如下:

network = tf.keras.models.Sequential()

embedding_layer = tf.keras.layers.Embedding(input_dim=len(NODE_MAP), 
                                        output_dim=30,
                                        input_length=256,
                                        embeddings_initializer=tf.keras.initializers.RandomUniform())
network.add(embedding_layer)

hidden_layer = tf.keras.layers.Dense(100, activation='tanh')
network.add(hidden_layer)

softmax_layer = tf.keras.layers.Softmax()
network.add(softmax_layer)

network.compile(optimizer='SGD', loss='categorical_crossentropy')

But this code raises the "ValueError: Shapes (None, 256) and (None, 256, 100) are incompatible" error.但是此代码引发了“ValueError:Shapes (None, 256) and (None, 256, 100) are incompatible”错误。 If I add an extra Flatten layer between the Embedding and Dense layers the error changes to "ValueError: Shapes (None, 256) and (None, 100) are incompatible".如果我在 Embedding 和 Dense 层之间添加一个额外的 Flatten 层,错误将更改为“ValueError:Shapes (None, 256) and (None, 100) are incompatible”。 Then if I change the number of units in Dense layer from 100 to 256 the network starts to work, but doesn't learn (the training process does not improve the accuracy).然后,如果我将密集层中的单元数从 100 更改为 256,网络开始工作,但不会学习(训练过程不会提高准确性)。 What am I missing?我错过了什么?

Change loss function to sparce_cathegorical_entropy:将损失 function 更改为 sparce_cathegorical_entropy:

network.compile(optimizer='SGD', loss='sparce_categorical_crossentropy')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM