如何将这个代码从Keras转换成Tensorflow?

2024-05-14 06:23:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将Keras代码转换为tensorflow,我对Keras api不太了解,我是tensorflow用户,Keras代码如下:

rawmeta = layers.Input(shape=(1,), dtype="string")
emb = elmolayer()(rawmeta)
d1 = layers.Dense(256, activation='relu')(emb)
yhat = layers.Dense(31, activation='softmax', name = "output_node")(d1)
model = Model(inputs=[rawmeta], outputs=yhat)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

其中elmolayer的定义如下:

class elmolayer(Layer):

    def __init__(self, **kwargs):
        self.dimensions = 1024
        self.trainable=True
        super(elmolayer, self).__init__(**kwargs)

    def build(self, input_shape):
        self.elmo = hub.Module('https://tfhub.dev/google/elmo/2', trainable=self.trainable,
                               name="{}_module".format(self.name))

        self.trainable_weights += K.tf.trainable_variables(scope="^{}_module/.*".format(self.name))
        super(elmolayer, self).build(input_shape)

    def call(self, x, mask=None):
        result = self.elmo(K.squeeze(K.cast(x, tf.string), axis=1),
                      as_dict=True,
                      signature='default',
                      )['default']
        return result

    def compute_mask(self, inputs, mask=None):
        return K.not_equal(inputs, '--PAD--')

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.dimensions)

我对这段代码的Tensorflow实现是:

class Base_model(object):

    def __init__(self, elmo_embedding_matrix):

        tf.reset_default_graph()

        # define placeholders
        sentences        = tf.placeholder(tf.int32, [None, None], name='sentences')
        y_true     = tf.placeholder(tf.int32, [None, None], name='labels'  )





        self.elmo     = tf.get_variable(name="relation_embedding", shape=[elmo_embedding_matrix.shape[0],elmo_embedding_matrix.shape[1]],
                                         initializer=tf.constant_initializer(np.array(elmo_embedding_matrix)), 
                                         trainable=True,dtype=tf.float32)

        embedding_lookup   = tf.nn.embedding_lookup(self.elmo,sentences)

        d1 = tf.layers.dense(embedding_lookup, 256, tf.nn.relu)

        y_pred = tf.layers.dense(d1, 31, tf.nn.softmax)


        matches = tf.equal(tf.argmax(y_pred,1),tf.argmax(y_true,1))
        acc = tf.reduce_mean(tf.cast(matches,tf.float32))

        cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_true,logits=y_pred))
        train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cross_entropy)

我的困惑是keras模型中最后一个致密层是:

yhat = layers.Dense(31, activation='softmax', name = "output_node")(d1)

在tensorflow代码中,如果我使用tf.nn.softmax_cross_entropy_with_logits_v2,那么我应该把第二个密集层传递给softmax,例如

y_pred = tf.layers.dense(d1, 31, tf.nn.softmax)

因为如果我在这里使用softmax,那么tf.nn.softmax_cross_entropy_with_logits_v2将在logits上再次使用softmax。你知道吗

如何将Keras代码转换成Tensorflow?你知道吗


Tags: 代码nameselfnonelayerstfdefnn