首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >工作代码:简单神经网络(‘y=2*x’)

工作代码:简单神经网络(‘y=2*x’)
EN

Stack Overflow用户
提问于 2021-09-07 02:26:59
回答 1查看 101关注 0票数 1

我是一个神经网络和机器学习的初学者(因此,我希望模型学习的是简单的y = 2 * x )。我首先解释我所知道的,最后给出一个最小的工作例子。

生成器函数只输出成对(x, y),其中y = 2 * x分批为batch_size组。

代码语言:javascript
复制
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

  • input
  • multiplier层:tf.keras.layers.Dense(1) (其中权重应该是来自y = 2 * x)
  • output

2 )

要运行下面的代码,您只需要pip install tensorflow==2.6.0

最小工作示例(运行,但精度总是接近0):

代码语言:javascript
复制
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_testingbatch_size小于generator_trainingbatch_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

EN

回答 1

Stack Overflow用户

发布于 2021-09-07 05:13:02

对于这样的任务,不需要一个生成器--您可以直接通过numpy数组

  • ,生成器也是一个无限循环,因此模型不断获得新数据,并且保持在相同的时代--
  • --模型也太简单了。用于改进模型的
  • 必须根据以前遇到的训练数据来更新其权重,但是由于生成器不断生成新的数据,并且它停留在第一个时代,所以没有任何改进。

f 29

这里的数据是整数的,但是模型会给出浮动类型的预测,因此比较是不可靠的,因此精度不是performance.

下面的代码

  • A dataGenerator使用顺序API
  • 使用不同的模型
  • 添加激活函数到层
  • 使用均方误差损失函数

F 228

代码语言:javascript
复制
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))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69081769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档