简体   繁体   中英

AttributError: 'NoneType' object has no attribute 'dtype'

I am building a simple recurrent neural network, which contains a lstm layer with a fully connected layer, to make the classification for every row data. shape of my data 'x_train' is an nd.array with the shape of (210,240,1), 'y_train' is an nd.array with the shape of (210,). And the model output is normal. However, when I run model.fit(), there is always an error: AttributeError: 'NoneType' object has no attribute 'dtype'.

I don' t know what's wrong with the following code.

#%%
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf
import pandas as pd
import numpy as np

#%%
model = tf.keras.models.Sequential([
  tf.keras.layers.LSTM(8),
  tf.keras.layers.Dense(units = 2)
])

#%%
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy\
    (from_logits=True)
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),\
              loss = loss_fn)

#%%
x_train = np.random.randn(210,240,1)
y_train = np.random.binomial(1, 0.5,(210,))

#%%
model.fit(x_train, y_train, epochs=20)

The following are the whole error information:

Traceback (most recent call last):

  File "D:\运筹优化\机器学习课程项目\时序数据预测\rnn_exploration.py", line 68, in <module>
    model.fit(x_train, y_train, epochs=20)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
    use_multiprocessing=use_multiprocessing)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 235, in fit
    use_multiprocessing=use_multiprocessing)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 593, in _process_training_inputs
    use_multiprocessing=use_multiprocessing)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 646, in _process_inputs
    x, y, sample_weight=sample_weights)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2360, in _standardize_user_data
    self._compile_from_inputs(all_inputs, y_input, x, y)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2618, in _compile_from_inputs
    experimental_run_tf_function=self._experimental_run_tf_function)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 416, in compile
    endpoint.create_training_target(t, run_eagerly=self.run_eagerly)

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 3023, in create_training_target
    self.loss_fn, K.dtype(self.output))

  File "D:\python\anaconda\anaconda\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\backend.py", line 1237, in dtype
    return x.dtype.base_dtype.name

AttributeError: 'NoneType' object has no attribute 'dtype'

Any help would be appreciated!

问题仍然存在于 numpy 1.21.3,降级到 1.19.5 有效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM