Tensorflow 找不到节点的有效设备。即使在转换为 float32 之后

Tensorflow cannot find valid device for node. even after casting to float32

您好,我在尝试 运行 我的模型时遇到错误

这是我的 __init__ 方法 -

class model(Model):
    def __init__(self):
        super(model, self).__init__()
        self.lr = 0.01
        self.conv1 = Conv2D(filters=32, input_shape=(210, 160, 1), kernel_size=(3, 3), strides=1, padding='same', activation='elu')#(self.inp)

        self.conv2 = Conv2D(filters=32, kernel_size=(3, 3), strides=1, padding='same', activation='elu')#(self.conv1)
        self.mp2 = MaxPool2D(pool_size=(3, 3), strides=1, padding='same')#(self.conv2)

        self.conv3 = Conv2D(filters=64, kernel_size=(3, 3), strides=1, padding='same', activation='elu')#(self.mp2)
        self.mp3 = MaxPool2D(pool_size=(3, 3), strides=1, padding='same')#(self.conv3)

        self.conv4 = Conv2D(filters=64, kernel_size=(3, 3), strides=1, padding='same', activation='elu')#(self.mp3)
        self.mp4 = MaxPool2D(pool_size=(3, 3), strides=1, padding='same')#(self.conv4)

        self.flat = Flatten() #(self.mp6)
        self.value = Dense(1, activation=None)#(self.flat) # how good is a particular state
        self.advantage = Dense(env.action_space.n, activation=None)#(self.flat) # which is best action
        self.compile(optimizer=Adam(lr=self.lr), loss='mse', metrics=['accuracy'])

然后我有一个名为 predict_advantage 的函数,我在其中遇到错误 -

def predict_advantage(self, state):
        state = tf.cast(cv2.cvtColor(state, cv2.COLOR_RGB2GRAY), tf.float32)
        #x = self.inp(state)
        x = self.conv1(x)

        x=self.conv2(x)
        x=self.mp2(x)

        x=self.conv3(x)
        x=self.mp3(x)

        x=self.conv4(x)
        x=self.mp4(x)

        x = self.flat(x)
        # value = self.value(x)
        x = self.advantage(x)
        return x
tensorflow.python.framework.errors_impl.NotFoundError: Could not find valid device for node.
Node:{{node MatMul}}

顺便说一句,它还打印出某个特定层或所有层的设备和数据类型。我不知道它做了什么,但它在这里 -

All kernels registered for op MatMul :
  device='GPU'; T in [DT_FLOAT]
  device='GPU'; T in [DT_DOUBLE]
  device='GPU'; T in [DT_COMPLEX64]
  device='GPU'; T in [DT_COMPLEX128]
  device='GPU'; T in [DT_HALF]
  device='CPU'; label='eigen'; T in [DT_FLOAT]
  device='CPU'; label='eigen'; T in [DT_DOUBLE]
  ..........
  ..........
  ..........
  device='CPU'; T in [DT_COMPLEX64]
  device='CPU'; T in [DT_COMPLEX128]
  device='GPU'; label='cublas'; T in [DT_FLOAT]
  device='GPU'; label='cublas'; T in [DT_DOUBLE]
  device='GPU'; label='cublas'; T in [DT_COMPLEX64]
  device='GPU'; label='cublas'; T in [DT_COMPLEX128]
  device='GPU'; label='cublas'; T in [DT_HALF]
 [Op:MatMul] name: dense_1/Tensordot/MatMul/

据我所知,我认为它误导了我,因为 gpu 上的东西无法与 cpu 上的东西交互。那么为什么它会在不同的设备上保留我的参数。

编辑:

这是完整代码的 link - https://pastebin.com/sd8L2xAM 如果您想找到它发生在哪一行,这也是我得到的完整错误 - https://pastebin.com/C9Dy5NxL

似乎该错误是类型不匹配的一般错误。

在下面的函数中,问题是您传递的状态类型是 NumPy 数组。 这会导致类型不匹配。由于self.model.advantage是一个密集层, 因此,将状态从 NumPy array 转换为 Tensor将解决类型问题。

def choose_action(self, state):
      if np.random.random() < self.epsilon:
          action = np.random.choice(env.action_space.n)
      else: # we exploit
          print(type(state)) ##nd array which is mismatch
          state = tf.cast(state, dtype=tf.float32) ## cast the state to a tensor
          actions = self.model.advantage(state)
          action = np.argmax(actions, axis=1)
      return action