我是一个神经网络和机器学习的初学者(因此,我希望模型学习的是简单的y = 2 * x )。我首先解释我所知道的,最后给出一个最小的工作例子。
生成器函数只输出成对(x, y),其中y = 2 * x分批为batch_size组。
generator = data_generator(batch_size=3)
print(next(generator))
# (
# array([[0], [1], [2]]),
# array([[0], [2], [4]]),
# )
print(next(generator))
# (
# array([[3], [4], [5]]),
# array([[6], [8], [10]]),
# )
print(next(generator))
# (
# array([ [6], [7], [8]]),
# array([[12], [14], [16]]),
# )由于这是一个线性函数,所以该模型只需要一个单层就可以学习y = 2 * x。
tf.keras.layers.Dense(1) (其中权重应该是来自y = 2 * x)的2 )
要运行下面的代码,您只需要pip install tensorflow==2.6.0
最小工作示例(运行,但精度总是接近0):
import random
import tensorflow as tf
import numpy as np
def data_generator(batch_size):
start = 0
end = start + batch_size
while True:
x = np.arange(start, end)
y = 2 * np.arange(start, end)
x = np.expand_dims(x, axis=-1)
y = np.expand_dims(y, axis=-1)
yield x, y
start += batch_size
end = start + batch_size
# Randomly divide so that we get repeated data points
# if random.random() < 0.5:
# start //= 2
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(1,)),
tf.keras.layers.Dense(1),
])
model.compile(
loss=tf.keras.losses.BinaryCrossentropy(),
optimizer=tf.keras.optimizers.Adam(),
metrics=['accuracy'],
)
batch_size = 32
generator_training = data_generator(batch_size)
generator_testing = data_generator(batch_size)
model.fit(
x=generator_training,
validation_data=generator_testing,
)我试过以下几种方法来提高准确度,但没有用:
<
generator_testing的batch_size小于generator_training的batch_size,这样测试通常会针对已经在if random.random() < 0.5: start //= 2,这样训练就可以多次得到一些数据点--H 228H 129添加更多d30每个D33层中的神经元数>H 234<代码>H 135,将所有<代码>D36和D37值除以一个大数目(<代码>D38),这样D39>和代码>代码><>代码>代码之间。为什么训练给10e-4或更低的准确性,我应该做什么来使神经网络学习y = 2 * x
发布于 2021-09-07 05:13:02
对于这样的任务,不需要一个生成器--您可以直接通过numpy数组
f 29
这里的数据是整数的,但是模型会给出浮动类型的预测,因此比较是不可靠的,因此精度不是performance.
。
下面的代码
F 228
import random
import tensorflow as tf
import keras
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras import regularizers
from matplotlib import pyplot as plt
class DataGenerator(tf.keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, x, y, batch_size=32):
'Initialization'
self.x = x
self.y = y
self.batch_size = batch_size
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.x) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
data = self.x[index*self.batch_size:(index+1)*self.batch_size]
labels = self.y[index*self.batch_size:(index+1)*self.batch_size]
return data, labels
model = Sequential()
model.add(Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.001), input_shape = (1,)))
model.add(Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(Dense(1))
optimizer = tf.keras.optimizers.Adam()
model.compile(optimizer=optimizer,loss='mse', metrics=['loss'])
# Print the model summary
model.summary()
# train data
x = np.arange(0, batch_size * 100)
y = 2 * np.arange(0, batch_size * 100)
train = DataGenerator(x, y, batch_size)
n_epochs = 100
batch_size = 32
history = model.fit(train, batch_size=batch_size, epochs=n_epochs,)
# plot loss during training
plt.figure()
plt.title('Loss / Mean Squared Error')
plt.plot(history.history['loss'], label='train')
plt.legend()
plt.show()
plt.figure(figsize=(20, 5))
plt.subplot(1,3,1)
plt.xlabel('x')
plt.ylabel('y')
plt.plot(x, y,)
plt.title('Original data')
y_predict_train = tf.math.ceil(model.predict(x))
plt.subplot(1,3,2)
plt.plot(x, y_predict_train,)
plt.xlabel('x')
plt.ylabel('prediction')
plt.title('Predictions')
plt.show()
# test data
x_test = np.arange(batch_size * 1000, batch_size * 1500)
y_test = 2 * np.arange(batch_size * 1000, batch_size * 1500)
# floor gives the lower end of the floating point number ex. 4.560 will give 4
# you could also use ceil function ex 4.560 will give 5
y_predict_test = tf.math.floor(model.predict(x_test))
plt.figure(figsize=(20, 5))
plt.subplot(1,3,1)
plt.xlabel('x')
plt.ylabel('y')
plt.plot(x_test, y_test,)
plt.title('Test data')
plt.subplot(1,3,2)
plt.plot(x_test, y_predict_test,)
plt.xlabel('x')
plt.ylabel('prediction')
plt.title('Predictions')
plt.show()
q = model.predict( np.array( [10,200, 360, 1000] ) )
print(q)
print(tf.math.floor(q))https://stackoverflow.com/questions/69081769
复制相似问题