评估Tensorflow张量

问题描述 投票:2回答:1

为了得到输出相对于输入的梯度,可以使用

grads = tf.gradients(model.output, model.input)

grads =

[<tf.Tensor 'gradients_81/dense/MatMul_grad/MatMul:0' shape=(?, 18) dtype=float32>]

这是一个模型,有18个连续输入和1个连续输出。

我假设,这是一个符号表达式,并且需要一个包含18个条目的列表来将其提供给张量,这样它就可以将衍生物作为浮点数给出。

我会用

Test =[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]
with tf.Session() as sess:
    alpha = sess.run(grads, feed_dict = {model.input : Test})
    print(alpha)

但是我得到了错误

FailedPreconditionError (see above for traceback): Error while reading resource variable dense_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_2/bias)
     [[Node: dense_2/BiasAdd/ReadVariableOp = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_2/bias)]]

怎么了?

编辑:这是,之前发生的事情:

def build_model():
    model = keras.Sequential([ 
            ...])
    optimizer = ...
    model.compile(loss='mse'... ) 
    return model 


model = build_model()
history= model.fit(data_train,train_labels,...)
loss, mae, mse = model.evaluate(data_eval,...)

迄今取得的进展:

Test =[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]

with tf.Session() as sess:
    tf.keras.backend.set_session(sess)
    tf.initializers.variables(model.output)
    alpha = sess.run(grads, feed_dict = {model.input : Test})

也没有用,给出错误:

TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
tensorflow tensorboard tensorflow-datasets tensorflow-estimator
1个回答
2
投票

您正在尝试使用未初始化的变量。你所要做的就是添加

sess.run(tf.global_variables_initializer()) 

就在with tf.Session() as sess:之后

编辑:您需要注册与Keras的会话

with tf.Session() as sess:
    tf.keras.backend.set_session(sess)

并使用tf.initializers.variables(var_list)而不是tf.global_variables_initializer()

https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html

编辑:

Test = np.ones((1, 18), dtype=np.float32)

inputs = layers.Input(shape=[18,])
layer = layers.Dense(10, activation='sigmoid')(inputs)
model = tf.keras.Model(inputs=inputs, outputs=layer)
model.compile(optimizer='adam', loss='mse')
checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath='path/weights.hdf5')
model.fit(Test, nb_epoch=1, batch_size=1, callbacks=[checkpointer])
grads = tf.gradients(model.output, model.input)

with tf.Session() as sess:
    tf.keras.backend.set_session(sess)
    sess.run(tf.global_variables_initializer())
    model.load_weights('path/weights.hdf5')
    alpha = sess.run(grads, feed_dict={model.input: Test})
    print(alpha)

这显示了一致的结果

© www.soinside.com 2019 - 2024. All rights reserved.