Keras Witf Tensorflow优化器

时间:2019-03-12 22:20:38

标签: tensorflow keras

我正在尝试使用Tensorflow后端优化器优化Keras模型。 我可以运行模型,计算损失并打印所有权重和偏差,但是当我尝试应用渐变时,模型会崩溃。

PS:我使用的是tensorflow Cuda docker容器,主机系统是Ubuntu 18.04.2 LTS。

代码:

import numpy as np
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dense, Input
from tensorflow.python.keras.optimizers import Adam

from tensorflow.python.keras import backend as K

import tensorflow as tf

data = np.random.uniform(low=0, high=5, size=50)
Y_Data = np.random.randint(low=0, high=3, size=(50, 3))


config = tf.ConfigProto(allow_soft_placement=True)
sess = tf.Session(config=config)
K.set_session(sess)

input = Input(shape=(1, ))
dens_1 = Dense(128, activation='relu')(input)
output = Dense(3, activation='linear')(dens_1)


Y_data_placeholder = tf.placeholder(tf.float32, shape=[None, 3])
loss = tf.losses.absolute_difference(Y_data_placeholder, output)

params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
grads = tf.gradients(loss, params)
grads = list(zip(grads, params))

trainer = tf.train.RMSPropOptimizer(learning_rate=0.003, decay=0.99, epsilon=1e-5)
_train = trainer.apply_gradients(grads)

model = Model(inputs=input, outputs=output)
adam = Adam(lr=0.001)
model.compile(loss="mse", optimizer=adam)

model.summary()

print('')
print(model.predict(data)) # Runs fine.

print('')
print(sess.run(output, feed_dict={input: data.reshape(50, 1)})) # Runs fine.

out_data, out_loss, out_params = sess.run([output, loss, params], feed_dict={input: data.reshape(50, 1), Y_data_placeholder: Y_Data}) # Runs fine.

print('')
print(out_params)

print('')
print(out_data)

print('')
print(out_loss)

out_data, out_loss, out_train = sess.run([output, loss, _train], feed_dict={input: data.reshape(50, 1), Y_data_placeholder: Y_Data}) # Runs Error.

print('')
print(out_train)

错误:

2019-03-12 21:59:20.737232: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737326: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737359: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737436: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at training_ops.cc:2933 : Not found: Resource localhost/dense_1/bias/RMSProp/N10tensorflow3VarE does not exist.
2019-03-12 21:59:20.737524: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737569: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737605: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737647: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at training_ops.cc:2933 : Not found: Resource localhost/dense_1/kernel/RMSProp/N10tensorflow3VarE does not exist.
2019-03-12 21:59:20.737896: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737944: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.737983: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.738050: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at training_ops.cc:2933 : Not found: Resource localhost/dense/bias/RMSProp/N10tensorflow3VarE does not exist.
2019-03-12 21:59:20.738110: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.738149: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.738186: W tensorflow/core/framework/op_kernel.cc:1261] Internal: Invalid variable reference.
2019-03-12 21:59:20.738231: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at training_ops.cc:2933 : Not found: Resource localhost/dense/kernel/RMSProp/N10tensorflow3VarE does not exist.

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InternalError: Invalid variable reference.
    [[{{node RMSProp/update_dense_1/bias/ResourceApplyRMSProp}} = ResourceApplyRMSProp[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/bias, dense_1/bias/RMSProp, dense_1/bias/RMSProp_1, RMSProp/learning_rate, RMSProp/decay, RMSProp/momentum, RMSProp/epsilon, gradients/dense_1/BiasAdd_grad/BiasAddGrad)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/project/test6.py", line 50, in <module>
     out_data, out_loss, out_train = sess.run([output, loss, _train], feed_dict={input: data.reshape(50, 1), Y_data_placeholder: Y_Data}) # Runs Error.
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1328, in _do_run
    run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InternalError: Invalid variable reference.
 [[node RMSProp/update_dense_1/bias/ResourceApplyRMSProp (defined at /opt/project/test6.py:31)  = ResourceApplyRMSProp[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/bias, dense_1/bias/RMSProp, dense_1/bias/RMSProp_1, RMSProp/learning_rate, RMSProp/decay, RMSProp/momentum, RMSProp/epsilon, gradients/dense_1/BiasAdd_grad/BiasAddGrad)]]

Caused by op 'RMSProp/update_dense_1/bias/ResourceApplyRMSProp', defined at:
  File "/opt/project/test6.py", line 31, in <module>
    _train = trainer.apply_gradients(grads)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 610, in apply_gradients
    update_ops.append(processor.update_op(self, grad))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 167, in update_op
    update_op = optimizer._resource_apply_dense(g, self._v)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/rmsprop.py", line 194, in _resource_apply_dense
    use_locking=self._use_locking)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/gen_training_ops.py", line 2073, in resource_apply_rms_prop
    use_locking=use_locking, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3274, in create_op
    op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1770, in __init__
    self._traceback = tf_stack.extract_stack()

InternalError (see above for traceback): Invalid variable reference.
 [[node RMSProp/update_dense_1/bias/ResourceApplyRMSProp (defined at /opt/project/test6.py:31)  = ResourceApplyRMSProp[T=DT_FLOAT, use_locking=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/bias, dense_1/bias/RMSProp, dense_1/bias/RMSProp_1, RMSProp/learning_rate, RMSProp/decay, RMSProp/momentum, RMSProp/epsilon, gradients/dense_1/BiasAdd_grad/BiasAddGrad)]]

1 个答案:

答案 0 :(得分:0)

我为之奋斗...

定义模型,损失和Tensorflow优化器之后,我需要首先初始化所有变量。

sess.run(tf.global_variables_initializer())