python Keras通过NN从向量预测标量值

46scxncf  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(85)

我想从给定的向量(大小=250,二进制元素)中预测标量值(介于0和100之间)。我有一个数据集,它包含1000个x值和1000个y值:

>>>in_.shape
(1000, 250)

>>>in_[0]
array([1, 0, 1, 1, 1, 1, 1, 1, ...])

>>>out.shape
(1000,)

>>>out[0]
64.46677867594474

我写了一个模型,但它似乎不工作。下面是代码片段,您可以在这里找到数据集https://ufile.io/f/yt61u

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

in_ = np.load('input.npy')
out = np.load('output.npy')

model = keras.Sequential([
        keras.Input(shape=(250,)),
        layers.Dense(1000, activation='relu'),
        layers.Dense(1000, activation='relu'),
        layers.Dense(250, activation='relu'),
        layers.Dense(1, activation='linear')])

model.compile(loss='binary_crossentropy', optimizer='adam',
              metrics=['accuracy'])

model.fit(in_, out, batch_size=100, epochs=5, validation_split=0.1)

如何改善这一点?

Epoch 1/5
9/9 [==============================] - 2s 54ms/step - loss: -240.3843 - accuracy: 0.0000e+00 - val_loss: -54.9291 - val_accuracy: 0.0000e+00
Epoch 2/5
9/9 [==============================] - 0s 18ms/step - loss: -311.0255 - accuracy: 0.0000e+00 - val_loss: -54.9291 - val_accuracy: 0.0000e+00
Epoch 3/5
9/9 [==============================] - 0s 20ms/step - loss: -311.0255 - accuracy: 0.0000e+00 - val_loss: -54.9291 - val_accuracy: 0.0000e+00
Epoch 4/5
9/9 [==============================] - 0s 18ms/step - loss: -311.0254 - accuracy: 0.0000e+00 - val_loss: -54.9291 - val_accuracy: 0.0000e+00
Epoch 5/5
9/9 [==============================] - 0s 18ms/step - loss: -311.0255 - accuracy: 0.0000e+00 - val_loss: -54.9291 - val_accuracy: 0.0000e+00
zbq4xfa0

zbq4xfa01#

这是可行的:

model = tf.keras.Sequential([tf.keras.Input(shape=(250,)),
                             tf.keras.layers.Dense(1000, activation='relu'),
                             tf.keras.layers.Dense(250, activation='relu'),
                             tf.keras.layers.Dense(250, activation='relu'),
                             tf.keras.layers.Dense(44, activation='relu'),
                             tf.keras.layers.Dense(1, activation='relu')])

model.compile(optimizer='sgd', loss='mse',
              metrics=tf.keras.metrics.MeanAbsolutePercentageError())

model.fit(in_, out, batch_size=1, epochs=25)

相关问题