【已解决】TF2.2.0+python>3.8下LSTM报错

文章讲述了在使用LSTM模型时遇到的两个错误:1.ValueError关于Sequential模型的单输出要求,解决方案是移除return_state=True。2.NotImplementedError与numpy和Tensor转换有关,提供了解决方案,包括降级numpy版本、Python版本或修改array_ops.py中的reduce_prod方法。通过这些方法可以成功运行代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【报错1】

LSTM ValueError: All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API.

【解决办法】

检查LSTM代码中是否有:return_state=True, 如果有,删掉这个就可以了。

【报错2】

NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array.

【解决办法】

1.降低numpy的版本到1.19.5 (不推荐)
2.降低Python的版本到3.6 (不推荐)
3.修改array_ops.py (墙裂推荐)

修改方法如下:

1. 根据运行的报错,找到array_ops.py文件(建议直接从pycharm报错处点链接进去)

2. import处添加

from tensorflow.python.ops.math_ops import reduce_prod 

3. 修改语句

源文件代码:

def _constant_if_small(value, shape, dtype, name):
  try:
    if np.prod(shape) < 1000:
      return constant(value, shape=shape, dtype=dtype, name=name)
  except TypeError:
    # Happens when shape is a Tensor, list with Tensor elements, etc.
    pass
  return None

修改后的代码:

def _constant_if_small(value, shape, dtype, name):
  try:
#    if np.prod(shape) < 1000:
     if reduce_prod(shape) < 1000: #修改之处
      return constant(value, shape=shape, dtype=dtype, name=name)
  except TypeError:
    # Happens when shape is a Tensor, list with Tensor elements, etc.
    pass
  return None

4. 重新运行,成功!

def main(): t0 = time.time() ​ # 选择模型 model = build_lstm_model() ​ # 编译模型 model.compile(optimizer=tf.keras.optimizers.Adam(0.001), loss=tf.keras.losses.BinaryCrossentropy(), metrics=[&#39;accuracy&#39;]) ​ # 训练模型 checkpoint = ModelCheckpoint(&#39;model_checkpoint.h5&#39;, save_weights_only=True, verbose=1, save_freq=&#39;epoch&#39;) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, callbacks=[checkpoint]) ​ # 评估模型 loss, accuracy = model.evaluate(x_test, y_test) print(f"Test Loss: {loss}, Test Accuracy: {accuracy}") ​ t1 = time.time() print(f"模型运行的时间为:{t1 - t0:.2f} 秒") ​ if __name__ == &#39;__main__&#39;: main() 10秒 WARNING:tensorflow:From /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/initializers.py:118: calling RandomUniform.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor WARNING:tensorflow:From /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1623: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers. NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array. --------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) Cell In[21], line 24 21 print(f"模型运行的时间为:{t1 - t0:.2f} 秒") 23 if __name__ == &#39;__main__&#39;: ---> 24 main() Cell In[21], line 5, in main() 2 t0 = time.time() 4 # 选择模型 ----> 5 model = build_lstm_model() 7 # 编译模型 8 model.compile(optimizer=tf.keras.optimizers.Adam(0.001), 9 loss=tf.keras.losses.BinaryCrossentropy(), 10 metrics=[&#39;accuracy&#39;]) Cell In[15], line 4, in build_lstm_model() 3 def build_lstm_model(): ----> 4 model = keras.Sequential([ 5 layers.Embedding(total_words, embedding_len, input_length=max_review_len), 6 layers.LSTM(64, return_sequences=False), 7 layers.Dense(1, activation=&#39;sigmoid&#39;) 8 ]) 9 return model File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/training/tracking/base.py:457, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/engine/sequential.py:113, in Sequential.__init__(self, layers, name) 111 tf_utils.assert_no_legacy_layers(layers) 112 for layer in layers: --> 113 self.add(layer) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/training/tracking/base.py:457, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/engine/sequential.py:195, in Sequential.add(self, layer) 190 self.inputs = layer_utils.get_source_inputs(self.outputs[0]) 192 elif self.outputs: 193 # If the model is being built continuously on top of an input layer: 194 # refresh its output. --> 195 output_tensor = layer(self.outputs[0]) 196 if len(nest.flatten(output_tensor)) != 1: 197 raise TypeError(&#39;All layers in a Sequential model &#39; 198 &#39;should have a single output tensor. &#39; 199 &#39;For multi-output layers, &#39; 200 &#39;use the functional API.&#39;) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:623, in RNN.__call__(self, inputs, initial_state, constants, **kwargs) 617 inputs, initial_state, constants = _standardize_args(inputs, 618 initial_state, 619 constants, 620 self._num_constants) 622 if initial_state is None and constants is None: --> 623 return super(RNN, self).__call__(inputs, **kwargs) 625 # If any of `initial_state` or `constants` are specified and are Keras 626 # tensors, then add them to the inputs and temporarily modify the 627 # input_spec to include them. 629 additional_inputs = [] File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/engine/base_layer.py:854, in Layer.__call__(self, inputs, *args, **kwargs) 852 outputs = base_layer_utils.mark_as_return(outputs, acd) 853 else: --> 854 outputs = call_fn(cast_inputs, *args, **kwargs) 856 except errors.OperatorNotAllowedInGraphError as e: 857 raise TypeError(&#39;You are attempting to use Python control &#39; 858 &#39;flow in a layer that was not declared to be &#39; 859 &#39;dynamic. Pass `dynamic=True` to the class &#39; 860 &#39;constructor.\nEncountered error:\n"""\n&#39; + 861 str(e) + &#39;\n"""&#39;) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:2548, in LSTM.call(self, inputs, mask, training, initial_state) 2546 self.cell.reset_dropout_mask() 2547 self.cell.reset_recurrent_dropout_mask() -> 2548 return super(LSTM, self).call( 2549 inputs, mask=mask, training=training, initial_state=initial_state) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:681, in RNN.call(self, inputs, mask, training, initial_state, constants) 675 def call(self, 676 inputs, 677 mask=None, 678 training=None, 679 initial_state=None, 680 constants=None): --> 681 inputs, initial_state, constants = self._process_inputs( 682 inputs, initial_state, constants) 684 if mask is not None: 685 # Time step masks must be the same for each input. 686 # TODO(scottzhu): Should we accept multiple different masks? 687 mask = nest.flatten(mask)[0] File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:798, in RNN._process_inputs(self, inputs, initial_state, constants) 796 initial_state = self.states 797 else: --> 798 initial_state = self.get_initial_state(inputs) 800 if len(initial_state) != len(self.states): 801 raise ValueError(&#39;Layer has &#39; + str(len(self.states)) + 802 &#39; states but was passed &#39; + str(len(initial_state)) + 803 &#39; initial states.&#39;) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:605, in RNN.get_initial_state(self, inputs) 603 dtype = inputs.dtype 604 if get_initial_state_fn: --> 605 init_state = get_initial_state_fn( 606 inputs=None, batch_size=batch_size, dtype=dtype) 607 else: 608 init_state = _generate_zero_filled_state(batch_size, self.cell.state_size, 609 dtype) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:2313, in LSTMCell.get_initial_state(self, inputs, batch_size, dtype) 2312 def get_initial_state(self, inputs=None, batch_size=None, dtype=None): -> 2313 return list(_generate_zero_filled_state_for_cell( 2314 self, inputs, batch_size, dtype)) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:2752, in _generate_zero_filled_state_for_cell(cell, inputs, batch_size, dtype) 2750 batch_size = array_ops.shape(inputs)[0] 2751 dtype = inputs.dtype -> 2752 return _generate_zero_filled_state(batch_size, cell.state_size, dtype) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:2768, in _generate_zero_filled_state(batch_size_tensor, state_size, dtype) 2765 return array_ops.zeros(init_state_size, dtype=dtype) 2767 if nest.is_sequence(state_size): -> 2768 return nest.map_structure(create_zeros, state_size) 2769 else: 2770 return create_zeros(state_size) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/util/nest.py:536, in map_structure(func, *structure, **kwargs) 532 flat_structure = [flatten(s, expand_composites) for s in structure] 533 entries = zip(*flat_structure) 535 return pack_sequence_as( --> 536 structure[0], [func(*x) for x in entries], 537 expand_composites=expand_composites) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/util/nest.py:536, in <listcomp>(.0) 532 flat_structure = [flatten(s, expand_composites) for s in structure] 533 entries = zip(*flat_structure) 535 return pack_sequence_as( --> 536 structure[0], [func(*x) for x in entries], 537 expand_composites=expand_composites) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/keras/layers/recurrent.py:2765, in _generate_zero_filled_state.<locals>.create_zeros(unnested_state_size) 2763 flat_dims = tensor_shape.as_shape(unnested_state_size).as_list() 2764 init_state_size = [batch_size_tensor] + flat_dims -> 2765 return array_ops.zeros(init_state_size, dtype=dtype) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/ops/array_ops.py:2338, in zeros(shape, dtype, name) 2334 if not isinstance(shape, ops.Tensor): 2335 try: 2336 # Create a constant if it won&#39;t be very big. Otherwise create a fill op 2337 # to prevent serialized GraphDefs from becoming too large. -> 2338 output = _constant_if_small(zero, shape, dtype, name) 2339 if output is not None: 2340 return output File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/ops/array_ops.py:2295, in _constant_if_small(value, shape, dtype, name) 2293 def _constant_if_small(value, shape, dtype, name): 2294 try: -> 2295 if np.prod(shape) < 1000: 2296 return constant(value, shape=shape, dtype=dtype, name=name) 2297 except TypeError: 2298 # Happens when shape is a Tensor, list with Tensor elements, etc. File <__array_function__ internals>:180, in prod(*args, **kwargs) File /opt/conda/lib/python3.8/site-packages/numpy/core/fromnumeric.py:3088, in prod(a, axis, dtype, out, keepdims, initial, where) 2970 @array_function_dispatch(_prod_dispatcher) 2971 def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, 2972 initial=np._NoValue, where=np._NoValue): 2973 """ 2974 Return the product of array elements over a given axis. 2975 (...) 3086 10 3087 """ -> 3088 return _wrapreduction(a, np.multiply, &#39;prod&#39;, axis, dtype, out, 3089 keepdims=keepdims, initial=initial, where=where) File /opt/conda/lib/python3.8/site-packages/numpy/core/fromnumeric.py:86, in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs) 83 else: 84 return reduction(axis=axis, out=out, **passkwargs) ---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs) File /opt/conda/lib/python3.8/site-packages/tensorflow_core/python/framework/ops.py:735, in Tensor.__array__(self) 734 def __array__(self): --> 735 raise NotImplementedError("Cannot convert a symbolic Tensor ({}) to a numpy" 736 " array.".format(self.name)) NotImplementedError: Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array.
06-22
Code 分享 Notebook 保存成功 Python 3 (ipykernel) import os import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from tensorflow.keras import layers,losses,optimizers, Sequential from tensorflow.keras.layers import InputLayer, Dense, LSTM, Dropout from tensorflow.keras.models import load_model from tensorflow.keras import Model 22025-06-21 17:14:21.232222: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2025-06-21 17:14:21.246790: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered WARNING: All log messages before absl::InitializeLog() is called are written to STDERR E0000 00:00:1750497261.261784 789 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered E0000 00:00:1750497261.266275 789 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered W0000 00:00:1750497261.278129 789 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1750497261.278144 789 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1750497261.278145 789 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1750497261.278147 789 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. 2025-06-21 17:14:21.282294: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. + Code + Markdown pip install tensorflow 26秒 Looking in indexes: https://mirrors.cloud.aliyuncs.com/pypi/simple Collecting tensorflow Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/ba/1c/370b5546cf7afc29649b2fb74c171ef2493a36f62cf901c1425ead4a56af/tensorflow-2.19.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (644.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 644.9/644.9 MB 8.5 MB/s eta 0:00:0000:0100:01 Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.11/site-packages (from tensorflow) (2.3.0) Collecting astunparse>=1.6.0 (from tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/2b/03/13dde6512ad7b4557eb792fbcf0c653af6076b81e5941d36ec61f7ce6028/astunparse-1.6.3-py2.py3-none-any.whl (12 kB) Requirement already satisfied: flatbuffers>=24.3.25 in /usr/local/lib/python3.11/site-packages (from tensorflow) (25.2.10) Collecting gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 (from tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/a3/61/8001b38461d751cd1a0c3a6ae84346796a5758123f3ed97a1b121dfbf4f3/gast-0.6.0-py3-none-any.whl (21 kB) Collecting google-pasta>=0.1.1 (from tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/a3/de/c648ef6835192e6e2cc03f40b19eeda4382c49b5bafb43d88b931c4c74ac/google_pasta-0.2.0-py3-none-any.whl (57 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.5/57.5 kB 11.0 MB/s eta 0:00:00 Collecting libclang>=13.0.0 (from tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/1d/fc/716c1e62e512ef1c160e7984a73a5fc7df45166f2ff3f254e71c58076f7c/libclang-18.1.1-py2.py3-none-manylinux2010_x86_64.whl (24.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.5/24.5 MB 121.7 MB/s eta 0:00:0000:0100:01 Collecting opt-einsum>=2.3.2 (from tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/23/cd/066e86230ae37ed0be70aae89aabf03ca8d9f39c8aea0dec8029455b5540/opt_einsum-3.4.0-py3-none-any.whl (71 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 71.9/71.9 kB 16.5 MB/s eta 0:00:00 Requirement already satisfied: packaging in /usr/local/lib/python3.11/site-packages (from tensorflow) (24.2) Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0dev,>=3.20.3 in /usr/local/lib/python3.11/site-packages (from tensorflow) (3.20.3) Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.11/site-packages (from tensorflow) (2.32.3) Requirement already satisfied: setuptools in /usr/local/lib/python3.11/site-packages (from tensorflow) (65.5.1) Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.11/site-packages (from tensorflow) (1.17.0) Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.11/site-packages (from tensorflow) (3.1.0) Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.11/site-packages (from tensorflow) (4.12.2) Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.11/site-packages (from tensorflow) (1.17.2) Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.11/site-packages (from tensorflow) (1.72.1) Requirement already satisfied: tensorboard~=2.19.0 in /usr/local/lib/python3.11/site-packages (from tensorflow) (2.19.0) Collecting keras>=3.5.0 (from tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/95/e6/4179c461a5fc43e3736880f64dbdc9b1a5349649f0ae32ded927c0e3a227/keras-3.10.0-py3-none-any.whl (1.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.4/1.4 MB 110.8 MB/s eta 0:00:00 Requirement already satisfied: numpy<2.2.0,>=1.26.0 in /usr/local/lib/python3.11/site-packages (from tensorflow) (1.26.4) Requirement already satisfied: h5py>=3.11.0 in /usr/local/lib/python3.11/site-packages (from tensorflow) (3.13.0) Collecting ml-dtypes<1.0.0,>=0.5.1 (from tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/cc/2a/5421fd3dbe6eef9b844cc9d05f568b9fb568503a2e51cb1eb4443d9fc56b/ml_dtypes-0.5.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.7/4.7 MB 144.7 MB/s eta 0:00:00 Collecting tensorflow-io-gcs-filesystem>=0.23.1 (from tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/66/7f/e36ae148c2f03d61ca1bff24bc13a0fef6d6825c966abef73fc6f880a23b/tensorflow_io_gcs_filesystem-0.37.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.1/5.1 MB 158.1 MB/s eta 0:00:00 Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.11/site-packages (from astunparse>=1.6.0->tensorflow) (0.45.1) Requirement already satisfied: rich in /usr/local/lib/python3.11/site-packages (from keras>=3.5.0->tensorflow) (13.9.4) Collecting namex (from keras>=3.5.0->tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/b2/bc/465daf1de06409cdd4532082806770ee0d8d7df434da79c76564d0f69741/namex-0.1.0-py3-none-any.whl (5.9 kB) Collecting optree (from keras>=3.5.0->tensorflow) Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/43/6e/3721bf455834a4cfef1ecd9410666ec1d5708b32f01f57da7c10c2297e09/optree-0.16.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (416 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 416.8/416.8 kB 69.7 MB/s eta 0:00:00 Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/site-packages (from requests<3,>=2.21.0->tensorflow) (3.4.1) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/site-packages (from requests<3,>=2.21.0->tensorflow) (3.10) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/site-packages (from requests<3,>=2.21.0->tensorflow) (2.3.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/site-packages (from requests<3,>=2.21.0->tensorflow) (2025.1.31) Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.11/site-packages (from tensorboard~=2.19.0->tensorflow) (3.8) Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.11/site-packages (from tensorboard~=2.19.0->tensorflow) (0.7.2) Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.11/site-packages (from tensorboard~=2.19.0->tensorflow) (3.1.3) Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.11/site-packages (from werkzeug>=1.0.1->tensorboard~=2.19.0->tensorflow) (3.0.2) Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.11/site-packages (from rich->keras>=3.5.0->tensorflow) (3.0.0) Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.11/site-packages (from rich->keras>=3.5.0->tensorflow) (2.19.1) Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich->keras>=3.5.0->tensorflow) (0.1.2) DEPRECATION: pytorch-lightning 1.7.7 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063 Installing collected packages: namex, libclang, tensorflow-io-gcs-filesystem, optree, opt-einsum, ml-dtypes, google-pasta, gast, astunparse, keras, tensorflow Successfully installed astunparse-1.6.3 gast-0.6.0 google-pasta-0.2.0 keras-3.10.0 libclang-18.1.1 ml-dtypes-0.5.1 namex-0.1.0 opt-einsum-3.4.0 optree-0.16.0 tensorflow-2.19.0 tensorflow-io-gcs-filesystem-0.37.1 WARNING: Running pip as the &#39;root&#39; user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv [notice] A new release of pip is available: 23.3.2 -> 25.1.1 [notice] To update, run: pip install --upgrade pip Note: you may need to restart the kernel to use updated packages. + Code + Markdown stock_data = pd.read_csv(&#39;IBM_stock_data.csv&#39;) 0+ Code + Markdown stock_data.head() 0秒 date Open High Low Close Volume Price Change % 0 1999/11/1 98.50 98.81 96.37 96.75 9551800 0.000000 1 1999/11/2 96.75 96.81 93.69 94.81 11105400 -2.005168 2 1999/11/3 95.87 95.94 93.50 94.37 10369100 -0.464086 3 1999/11/4 94.44 94.44 90.00 91.56 16697600 -2.977641 4 1999/11/5 92.75 92.94 90.19 90.25 13737600 -1.430756 + Code + Markdown scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(stock_data[&#39;Close&#39;].values.reshape(-1, 1)) 0+ Code + Markdown scaled_data[0] 0秒 array([0.23131139]) + Code + Markdown len(scaled_data) 0秒 6293 + Code + Markdown training_data_len = int(np.ceil(len(scaled_data) * 0.8)) #向上取整 0+ Code + Markdown train_data = scaled_data[0:training_data_len] X_train, y_train = [], [] time_step = 10 # 时间窗口,模型基于前10个时间步长进行预测。可以尝试不同长度(如2030)并观察效果变化。 0+ Code + Markdown for i in range(len(train_data) - time_step - 1): X_train.append(train_data[i:(i + time_step), 0]) y_train.append(train_data[i + time_step, 0]) 0+ Code + Markdown X_train, y_train = np.array(X_train), np.array(y_train) X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1) 0+ Code + Markdown X_train.shape 0秒 (5024, 10, 1) + Code + Markdown X_test, y_test = [], [] test_data = scaled_data[training_data_len - time_step:] ​ for i in range(len(test_data) - time_step): X_test.append(test_data[i:(i + time_step), 0]) y_test.append(test_data[i + time_step, 0]) ​ X_test, y_test = np.array(X_test), np.array(y_test) X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1) 0+ Code + Markdown model = Sequential() model.add(InputLayer(input_shape=(X_train.shape[1], 1))) model.add(LSTM(units=64, return_sequences=True)) # 调整Dropout: 当前设置为0.3,可以尝试在不同的层上使用不同的Dropout值,例如0.2~0.5之间。Dropout的作用是防止过拟合。 model.add(Dropout(0.3)) model.add(LSTM(units=64, return_sequences=True)) model.add(Dropout(0.3)) model.add(LSTM(units=32)) model.add(Dropout(0.2)) # 增加回归层: 如果希望更高的拟合精度,可以添加多个Dense层,例如在输出前再增加一层Dense。 model.add(Dense(units=1)) 0秒 /usr/local/lib/python3.11/site-packages/keras/src/layers/core/input_layer.py:27: UserWarning: Argument `input_shape` is deprecated. Use `shape` instead. warnings.warn( 2025-06-21 17:14:34.072049: E external/local_xla/xla/stream_executor/cuda/cuda_platform.cc:51] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303) + Code + Markdown def MyRNN(): model = Sequential([ layers.InputLayer(input_shape=(X_train.shape[1],1)), layers.SimpleRNN(units=64, dropout=0.5, return_sequences=True, unroll=True), layers.SimpleRNN(units=64, dropout=0.5, unroll=True), layers.Dense(1)] ) return model 0+ Code + Markdown model = MyRNN() model.compile(optimizer=&#39;adam&#39;, loss=&#39;mean_squared_error&#39;) 0+ Code + Markdown history = model.fit( X_train, y_train, epochs=2, # 批大小(batch_size): 尝试不同的batch_size,例如16、32、64,以找到训练稳定性和准确性之间的平衡 batch_size=32, #callbacks=[early_stopping, lr_scheduler] ) 2秒 Epoch 1/2 157/157 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.1122 Epoch 2/2 157/157 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0278 + Code + Markdown model.save(&#39;my_model.keras&#39;) 0+ Code + Markdown train_loss = model.evaluate(X_train, y_train, verbose=0) test_loss = model.evaluate(X_test, y_test, verbose=0) 1秒 + Code + Markdown # 训练集上计算的损失值,数值越小表示模型在训练数据上的拟合效果越好 print(f"Training Loss: {train_loss:.4f}") # 测试集上计算的损失值,反映了模型在未见过的数据上的表现。测试损失略高于训练损失,但差距不大,说明模型在新数据上的表现依然良好。 print(f"Testing Loss: {test_loss:.4f}") 0秒 Training Loss: 0.0459 Testing Loss: 0.0538 + Code + Markdown model = load_model(&#39;my_model.keras&#39;) predictions = model.predict(X_test) predictions = scaler.inverse_transform(predictions) # 反归一化预测值 0秒 40/40 ━━━━━━━━━━━━━━━━━━━━ 1s 8ms/step + Code + Markdown Code # 将数据分为训练集和验证集 train = stock_data[:training_data_len] valid = stock_data[training_data_len:] valid.loc[:, &#39;Predictions&#39;] = predictions # 将预测结果添加到验证集的 DataFrame 中 ​ # 绘制图像 plt.figure(figsize=(14, 5)) plt.title(&#39;Stock Price Prediction&#39;, fontsize=20) # 图表标题改为英文 plt.xlabel(&#39;Date&#39;, fontsize=14) # X 轴标签改为英文 plt.ylabel(&#39;Close Price&#39;, fontsize=14) # Y 轴标签改为英文 plt.plot(train[&#39;date&#39;], train[&#39;Close&#39;], label=&#39;Training Data&#39;, color=&#39;blue&#39;) # 训练数据标签改为英文 plt.plot(valid[&#39;date&#39;], valid[&#39;Close&#39;], label=&#39;Actual Price&#39;, color=&#39;green&#39;) # 真实价格标签改为英文 plt.plot(valid[&#39;date&#39;], valid[&#39;Predictions&#39;], label=&#39;Predicted Price&#39;, color=&#39;red&#39;) # 预测价格标签改为英文 plt.legend() # 添加图例 # 添加保存图像的代码 plt.savefig(&#39;stock_price_predictions.png&#39;) # 保存图像 plt.show() ​ # 计算和输出评估指标 rmse = np.sqrt(np.mean(np.square(predictions - y_test))) # 计算均方根误差 mae = np.mean(np.abs(predictions - y_test)) # 计算平均绝对误差 print(f&#39;RMSE (Root Mean Square Error): {rmse}, MAE (Mean Absolute Error): {mae}&#39;) # 输出信息改为英文 34秒 /tmp/ipykernel_789/422633826.py:4: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy valid.loc[:, &#39;Predictions&#39;] = predictions # 将预测结果添加到验证集的 DataFrame 中 RMSE (Root Mean Square Error): 102.69734326363911, MAE (Mean Absolute Error): 101.96265451052737误差好大在此代码基础上改进
最新发布
06-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

allein_STR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值