gpt4 book ai didi

python - Keras 网络没有学习

转载 作者:行者123 更新时间:2023-12-01 06:55:03 25 4
gpt4 key购买 nike

我正在尝试学习如何使用 Keras 创建神经网络,但网络无法学习。

这是我的测试,它应该只是学习输出输入,但这甚至不起作用。我刚刚将 X 数据复制到 Y 数据中,并将其输入到训练中。

即使我让它运行 1000 epoch 或更长时间,损失和准确性也不会改变。

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Dropout
import numpy

gpus = tf.config.experimental.list_physical_devices("GPU")
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices("GPU")
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)

# Input
X_daten = [
[-4],
[-3],
[-2],
[-1],
[ 0],
[ 1],
[ 2],
[ 3],
[ 4]
]
Y_daten = X_daten.copy()

test_anzahl = 2

X_train = numpy.array(X_daten[:-test_anzahl])
Y_train = numpy.array(Y_daten[:-test_anzahl])
X_test = numpy.array(X_daten[-test_anzahl:])
Y_test = numpy.array(Y_daten[-test_anzahl:])

print("1 X_train ", X_train.shape)
print("1 Y_train ", Y_train.shape)
print("1 X_test ", X_test.shape)
print("1 Y_test ", Y_test.shape)
print("-"*20)

X_train = numpy.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
Y_train = numpy.reshape(Y_train, (Y_train.shape[0], 1, Y_train.shape[1]))
X_test = numpy.reshape(X_test , ( X_test.shape[0], 1, X_test.shape[1]))
Y_test = numpy.reshape(Y_test , ( Y_test.shape[0], 1, Y_test.shape[1]))

print("2 X_train ", X_train.shape)
print("2 Y_train ", Y_train.shape)
print("2 X_test ", X_test.shape)
print("2 Y_test ", Y_test.shape)
print("-"*20)

# Neural Netzwerk
neuronen = 100
layer = 2
batch_size = 10
epoch = 1000
input_anzahl = 1
output_anzahl = 1
#dropout = 0.3
activation = "sigmoid"
optimizer = "Adam"

model = Sequential()

model.add(Dense(neuronen, input_shape=(None, input_anzahl), activation=activation))

for _ in range(layer):
model.add(Dense(neuronen, activation=activation))
#model.add(Dropout(dropout))

model.add(Dense(output_anzahl, activation=activation)) # Output

model.compile(loss="mean_squared_error", optimizer=optimizer, metrics=["accuracy"])

# Training
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epoch, verbose=1, shuffle=True, validation_data=(X_test, Y_test))

# Predict
print("\n" + "-"*100)
for i in range(len(X_daten)):
daten = numpy.array([[X_daten[i]]])
daten = numpy.reshape(daten, (daten.shape[0], 1, daten.shape[1]))

ergebnis = model.predict(daten)

print(" Ergebnis ", ergebnis, " \tY_daten[" + str(i) + "] ", Y_daten[i], " \tX_daten[" + str(i) + "] ", X_daten[i])

任何帮助将不胜感激:)

最佳答案

我建议首先将激活设置为“线性”。我相信 Sigmoid 被限制在 (0,+1) 范围内,这阻止了网络生成接近目标的值。这可能会让您朝着正确的方向开始。我希望这会有所帮助。

关于python - Keras 网络没有学习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58847723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com