将Tensorflow模型重写为Keras的最直接方法是?

时间:2018-10-30 05:31:17

标签: python tensorflow keras

将此张量流模型转换为Keras的最直接方法是什么?

这是我的图

batch_size = 1024

embedding_size = 500 # 2^8 Dimension of the embedding vector. Crashed at 158 for Embed size 2656016. So possible values are 154-157. Possible choices 154, 156
num_inputs =5

num_sampled = 128 # Number of negative examples to sample.

graph = tf.Graph()

with graph.as_default(): 

    train_dataset = tf.placeholder(tf.int32, shape=[batch_size, num_inputs ])
    train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])

    epochCount = tf.get_variable( 'epochCount', initializer= 0) #to store epoch count to total # of epochs are known
    update_epoch = tf.assign(epochCount, epochCount + 1)

    embeddings = tf.get_variable( 'embeddings', dtype=tf.float32,
        initializer= tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0, dtype=tf.float32) )

    softmax_weights = tf.get_variable( 'softmax_weights', dtype=tf.float32,
        initializer= tf.truncated_normal([vocabulary_size, embedding_size],
                             stddev=1.0 / math.sqrt(embedding_size), dtype=tf.float32 ) )

    softmax_biases = tf.get_variable('softmax_biases', dtype=tf.float32,
        initializer= tf.zeros([vocabulary_size], dtype=tf.float32),  trainable=False )

    embed = tf.nn.embedding_lookup(embeddings, train_dataset) #train data set is

    embed_reshaped = tf.reshape( embed, [batch_size*num_inputs, embedding_size] )

    segments= np.arange(batch_size).repeat(num_inputs)

    averaged_embeds = tf.segment_mean(embed_reshaped, segments, name=None)

    loss = tf.reduce_mean(
        tf.nn.sampled_softmax_loss(weights=softmax_weights, biases=softmax_biases, inputs=averaged_embeds, 
                                   sampled_values=tf.nn.uniform_candidate_sampler(true_classes=tf.cast(train_labels, tf.int64), num_sampled=num_sampled, num_true=1, unique=True, range_max=vocabulary_size, seed=None),
                                   labels=train_labels, num_sampled=num_sampled, num_classes=vocabulary_size)) 

    optimizer = tensorflow.contrib.opt.ShampooOptimizer(1.0).minimize(loss) 

我正在尝试遵循此示例

https://colab.research.google.com/github/tensorflow/tpu/blob/master/tools/colab/shakespeare_with_tpu_and_keras.ipynb#scrollTo=yLEM-fLJlEEt

但是在此过程中迷路了。

到目前为止,我的具体问题是:

  1. 如何处理占位符并将数据输入到我的模型中?

  2. 如何嵌入查找并平均嵌入?

  3. 您如何进行负采样? tf.nn.uniform_candidate_sampler的keras等价物?有没有办法确保阴性样本是唯一的并且与真实标签不同?

  4. 如何将损失转换为喀拉拉邦?

0 个答案:

没有答案