Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 能否使用Tensorflow Keras函数API模型训练Tensorflow变量?Tensorflow操作可以在函数API模型中使用吗?_Python_Tensorflow_Keras - Fatal编程技术网

Python 能否使用Tensorflow Keras函数API模型训练Tensorflow变量?Tensorflow操作可以在函数API模型中使用吗?

Python 能否使用Tensorflow Keras函数API模型训练Tensorflow变量?Tensorflow操作可以在函数API模型中使用吗?,python,tensorflow,keras,Python,Tensorflow,Keras,我想知道Keras模型是否使用tf.get_variable定义的函数API序列变量编译/训练?Keras培训是否也包括Tensorflow操作 所以基本上我想用Tensorflow变量和运算定义一个Keras模型,然后使用 model = tf.keras.Model(inputs=inputs, outputs=predictions) model.compile(optimizer=optimizer, loss=loss) model.fit(data, labels, batch_si

我想知道Keras模型是否使用
tf.get_variable
定义的函数API序列变量编译/训练?Keras培训是否也包括Tensorflow操作

所以基本上我想用Tensorflow变量和运算定义一个Keras模型,然后使用

model = tf.keras.Model(inputs=inputs, outputs=predictions)
model.compile(optimizer=optimizer, loss=loss)
model.fit(data, labels, batch_size=batch_size, epochs=epochs)
训练模型。原因是谷歌的TPU需要Keras或TF.Estimator API,Keras更受推荐,所以我想看看转换模型有多容易

背景 看起来,因为Tensorflow是后端,所以有多种方法可以混合Keras/Tensorflow变量。这篇博客文章展示了如何使用Tensorflow图/会话来训练Keras变量

这里还显示了Tensorflow变量可以作为Keras模型的输入

所以我想知道Keras是否可以训练Tensorflow变量

例子 我想在下面的Tensorflow架构中培训嵌入和softmax变量

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

  softmax_weights = tf.get_variable( 'softmax_weights',
    initializer= tf.truncated_normal([vocabulary_size, embedding_size],
                         stddev=1.0 / math.sqrt(embedding_size)))
  
  softmax_biases = tf.get_variable('softmax_biases', 
    initializer= tf.zeros([vocabulary_size]),  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,
                               labels=train_labels, num_sampled=num_sampled, num_classes=vocabulary_size))
由于Tensorflow Keras使用Tensorflow后端,我猜在某种程度上可以使用和训练Tensorflow变量,并在训练中使用Tensorflow操作

我为什么要这样做? Google的TPU要求您的架构通过Estimator API或Keras API实现。由于更推荐Keras API,因此可能有兴趣将常规Tensorflow图/会话转换为使用Keras API,并对其代码进行尽可能少的修改


了解如何结合Tensorflow操作并使用Keras模型编译/训练Tensorflow变量将大大有助于此。

此解决方案是否有帮助

您可以使用

model.add()
然后使用

model.layers[-1].trainable_weights.extend()
小背景:

正如我们所知,Keras是一个模型级库,为开发深度学习模型提供高级构建块

最重要的是:KerasAPI不处理张量运算。为此,它需要一个优化良好的张量操纵库,称为Keras的“后端引擎”

目前,Keras有三个后端引擎可用:TensorFlow后端(Google)、Theano后端和CNTK后端(MSFT)

了解如何结合Tensorflow操作,并使用Keras模型编译/训练Tensorflow变量,将大大有助于实现这一点

你唯一应该问自己的是Keras变量和正则Tensorflow变量之间的区别是什么

恰巧Keras变量有元数据。因此,为了使用Keras中的TensorFlow变量,需要进行转换

注:TensorFlow变量范围对Keras层或模型没有影响


最后,可以通过初始化Keras层(或模型)来实现变量共享。

谢谢!在你发布的链接中,它说“model.layers[-1]。trainable_weights.extend([W,b]),那么W是否类似于
W=tf.get_variable(…)
?此外,如果对变量W执行tensorflow操作,那么在Keras培训期间梯度会流经这些操作吗?因此它类似于
tf.Keras.backend.variable(tf.get_variable(…),…)
?此外,由于变量已转换为Keras变量,能否对该Keras变量执行Tensorflow操作?在Keras培训期间,梯度将通过所有操作流向变量?Tensorflow和Keras操作的混合是危险的,因为Keras构建了自己的图形,并且经常将任意Tensorflow代码插入其中是行不通的。谢谢!我应该调查一下。除了“keras graph construction”之外,您还推荐查找哪些关键短语
model.add()
model.layers[-1].trainable_weights.extend()