如何在python中克服模型拟合的问题?



我正在学习机器学习。这是有趣的!

我对这个错误有一个疑问。我在下面分享代码和错误消息。请解决它…!非常感谢!如果sequential_4…

,则错误显示输入0层的值错误。
a=df4['age']
b=df4['growth']

X=np.array(a.values.tolist())
y=np.array(b.values.tolist())
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
import numpy
import tensorflow as tf
seed = 0
numpy.random.seed(seed)
tf.random.set_seed(3)
X_train, X_test, y_train, y_test = train_test_split(a, b,
test_size = 0.3, random_state=seed)
model = Sequential()
model.add(Dense(30, input_dim=17, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_sqaured_error',
optimizer='adam')
model.fit(X_train, y_train, validation_data= (X_test, y_test), epochs=200, batch_size=10)

错误消息时代1/200

ValueError                                Traceback (most recent call last)
<ipython-input-56-ffc8e137fb64> in <module>()
----> 1 model.fit(X_train, y_train, validation_data= (X_test, y_test), epochs=200, batch_size=10)
9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975           except Exception as e:  # pylint:disable=broad-except
976             if hasattr(e, "ag_error_metadata"):
--> 977               raise e.ag_error_metadata.to_exception(e)
978             else:
979               raise

ValueError: in user code:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step  **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:754 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:259 assert_input_compatibility
' but received input with shape ' + display_shape(x.shape))
ValueError: Input 0 of layer sequential_14 is incompatible with the layer: expected axis -1 of input shape to have value 17 but received input with shape (None, 1)

我能够使用下面所示的示例代码复制您的问题

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
X = np.random.random((1000,1))
y = np.random.random((1000,1))

X_train,X_test, y_train,y_test = train_test_split(X,y)

dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_data = dataset.shuffle(len(X_train)).batch(32)
train_data = train_data.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

valid_ds = tf.data.Dataset.from_tensor_slices((X_test, y_test))
model = Sequential()
model.add(Dense(30, input_dim=17, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_sqaured_error',
optimizer='adam')
model.fit(train_data, epochs=3, validation_data=valid_ds) 

输出:

Epoch 1/3
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-0e4d5121895c> in <module>()
30 
31 #model.fit(X_train, y_train, validation_data= (X_test, y_test), epochs=200, batch_size=10)
---> 32 model.fit(train_data, epochs=3, validation_data=valid_ds)
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975           except Exception as e:  # pylint:disable=broad-except
976             if hasattr(e, "ag_error_metadata"):
--> 977               raise e.ag_error_metadata.to_exception(e)
978             else:
979               raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step  **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:754 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:259 assert_input_compatibility
' but received input with shape ' + display_shape(x.shape))
ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected axis -1 of input shape to have value 17 but received input with shape (None, 1)

固定代码:

这里顺序模型的输入层必须设置为1而不是17,因为输入数据的形状是(None, 1)

可以将model.compile中的mean_sqaured_error指定为mse

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
X = np.random.random((1000,1))
y = np.random.random((1000,1))

X_train,X_test, y_train,y_test = train_test_split(X,y)

dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_data = dataset.shuffle(len(X_train)).batch(32)
train_data = train_data.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

valid_ds = tf.data.Dataset.from_tensor_slices((X_test, y_test))
model = Sequential()
model.add(Dense(30, input_dim=1, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse',
optimizer='adam')
model.fit(train_data, epochs=3, validation_data=valid_ds) 

输出:

Epoch 1/3
24/24 [==============================] - 1s 28ms/step - loss: 0.5050 - val_loss: 0.2758
Epoch 2/3
24/24 [==============================] - 0s 21ms/step - loss: 0.2704 - val_loss: 0.1908
Epoch 3/3
24/24 [==============================] - 0s 21ms/step - loss: 0.2047 - val_loss: 0.1454
<tensorflow.python.keras.callbacks.History at 0x7fc28239d2d0>

相关内容

  • 没有找到相关文章

最新更新