gpt4 book ai didi

tensorflow - 在TensorFlow张量上调用Keras模型但保持权重

转载 作者:行者123 更新时间:2023-12-04 03:48:28 25 4
gpt4 key购买 nike

Keras as a simplified interface to TensorFlow: tutorial中,他们描述了如何在TensorFlow张量上调用Keras模型。

from keras.models import Sequential

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=784))
model.add(Dense(10, activation='softmax'))

# this works!
x = tf.placeholder(tf.float32, shape=(None, 784))
y = model(x)

他们还说:

Note: by calling a Keras model, your are reusing both its architecture and its weights. When you are calling a model on a tensor, you are creating new TF ops on top of the input tensor, and these ops are reusing the TF Variable instances already present in the model.



我认为这是因为 y中模型的权重与模型中的权重相同。但是,对我而言,似乎重新初始化了结果Tensorflow节点中的权重。下面是一个最小的示例:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense
# Create model with weight initialized to 1
model = Sequential()
model.add(Dense(1, input_dim=1, kernel_initializer='ones',
bias_initializer='zeros'))
model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])

# Save the weights
model.save_weights('file')

# Create another identical model except with weight initialized to 0
model2 = Sequential()
model2.add(Dense(1, input_dim=1, kernel_initializer='zeros',
bias_initializer='zeros'))
model2.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
# Load the weight from the first model
model2.load_weights('file')
# Call model with Tensorflow tensor
v = tf.Variable([[1, ], ], dtype=tf.float32)
node = model2(v)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(node), model2.predict(np.array([[1, ], ])))
# Prints (array([[ 0.]], dtype=float32), array([[ 1.]], dtype=float32))

为什么我要这样做:

我想在另一个最小化方案中使用经过训练的网络,这是不允许在搜索空间中“惩罚”网络的地方。因此,如果您有不涉及此特定方法的想法,也将不胜感激。

最佳答案

终于找到答案了。问题中的示例存在两个问题。

1:

首先也是最明显的是,我调用了tf.global_variables_intializer()函数,该函数将重新初始化 session 中的所有变量。相反,我应该调用tf.variables_initializer(var_list),其中var_list是要初始化的变量列表。

2:

第二个问题是Keras没有使用与本地Tensorflow对象相同的 session 。这意味着要能够使用我的 session model2(v)运行tensorflow对象sess,需要重新初始化。 Keras as a simplified interface to tensorflow: Tutorial再次能够提供帮助

We should start by creating a TensorFlow session and registering it with Keras. This means that Keras will use the session we registered to initialize all variables that it creates internally.



import tensorflow as tf
sess = tf.Session()

from keras import backend as K
K.set_session(sess)

如果将这些更改应用于我的问题中提供的示例,我们将获得以下代码,该代码完全可以完成预期的工作。

from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense
sess = tf.Session()
# Register session with Keras
K.set_session(sess)
model = Sequential()
model.add(Dense(1, input_dim=1, kernel_initializer='ones',
bias_initializer='zeros'))
model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
model.save_weights('test')

model2 = Sequential()
model2.add(Dense(1, input_dim=1, kernel_initializer='zeros',
bias_initializer='zeros'))
model2.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
model2.load_weights('test')
v = tf.Variable([[1, ], ], dtype=tf.float32)
node = model2(v)
init = tf.variables_initializer([v, ])
sess.run(init)
print(sess.run(node), model2.predict(np.array([[1, ], ])))
# prints: (array([[ 1.]], dtype=float32), array([[ 1.]], dtype=float32))

结论:

这个教训是,在混合Tensorflow和Keras时,请确保所有内容都使用相同的 session 。

关于tensorflow - 在TensorFlow张量上调用Keras模型但保持权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46790506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com