平方(x ^ 2)逼近的神经网络 [英] Neural network for square (x^2) approximation

查看:166
本文介绍了平方(x ^ 2)逼近的神经网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是TensorFlow和数据科学的新手.我做了一个简单的模块,应该弄清楚输入和输出数字之间的关系.在这种情况下,x和x平方. Python中的代码:

I'm new to TensorFlow and Data Science. I made a simple module that should figure out the relationship between input and output numbers. In this case, x and x squared. The code in Python:

import numpy as np
import tensorflow as tf

# TensorFlow only log error messages.
tf.logging.set_verbosity(tf.logging.ERROR)

features = np.array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
                    9, 10], dtype = float)
labels = np.array([100, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36, 49, 64,
                    81, 100], dtype = float)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(units = 1, input_shape = [1])
])

model.compile(loss = "mean_squared_error", optimizer = tf.keras.optimizers.Adam(0.0001))
model.fit(features, labels, epochs = 50000, verbose = False)
print(model.predict([4, 11, 20]))

我尝试了不同数量的单位,并添加了更多图层,甚至使用了relu激活功能,但结果始终是错误的. 它可以与其他关系(例如x和2x)一起使用. 这是什么问题?

I tried a different number of units, and adding more layers, and even using the relu activation function, but the results were always wrong. It works with other relationships like x and 2x. What is the problem here?

推荐答案

您犯了两个非常基本的错误:

You are making two very basic mistakes:

  • 您的超简单模型(具有单个单元的单层网络)根本不符合神经网络的条件,更不用说深度学习"模型了(因为您的问题已被标记)
  • 类似地,您的数据集(仅有20个样本)也非常小

当然可以理解,如果神经网络要解决问题,甚至要像x*x一样简单",那么它就必须具有一定的复杂性.而当它们充满大型训练数据集时,它们真正发挥作用的地方.

It is certainly understood that neural networks need to be of some complexity if they are to solve problems even as "simple" as x*x; and where they really shine is when fed with large training datasets.

尝试求解此类函数逼近的方法不仅是列出(少数可能的)输入,然后将其与所需的输出一起输入到模型中;请记住,NN是通过示例而不是通过符号推理来学习的.例子越多越好.在类似情况下,我们通常要做的是生成大量示例,然后将它们提供给模型进行训练.

The methodology when trying to solve such function approximations is not to just list the (few possible) inputs and then fed to the model, along with the desired outputs; remember, NNs learn through examples, and not through symbolic reasoning. And the more examples the better. What we usually do in similar cases is to generate a large number of examples, which we subsequently feed to the model for training.

话虽如此,这是Keras中一个三层神经网络的简单演示,它使用[-50, 50]中生成的10,000个随机数作为输入,以逼近函数x*x:

Having said that, here is a rather simple demonstration of a 3-layer neural network in Keras for approximating the function x*x, using as input 10,000 random numbers generated in [-50, 50]:

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras import regularizers
import matplotlib.pyplot as plt

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))

model.compile(optimizer=Adam(),loss='mse')

# generate 10,000 random numbers in [-50, 50], along with their squares
x = np.random.random((10000,1))*100-50
y = x**2

# fit the model, keeping 2,000 samples as validation set
hist = model.fit(x,y,validation_split=0.2,
             epochs= 15000,
             batch_size=256)

# check some predictions:
print(model.predict([4, -4, 11, 20, 8, -5]))
# result:
[[ 16.633354]
 [ 15.031291]
 [121.26833 ]
 [397.78638 ]
 [ 65.70035 ]
 [ 27.040245]]

嗯,还不错!请记住,NN是函数逼近器:我们不应期望它们既精确重现函数关系,也不希望知道" 4-4的结果应该是完全相同.

Well, not that bad! Remember that NNs are function approximators: we should expect them neither to exactly reproduce the functional relationship nor to "know" that the results for 4 and -4 should be identical.

让我们在[-50,50]中生成一些新的随机数据(记住,出于所有实际目的,这些都是该模型的 unemeen 数据),并将它们与原始数据一起绘制以得到更多的随机数据.总体图片:

Let's generate some new random data in [-50,50] (remember, for all practical purposes, these are unseen data for the model) and plot them, along with the original ones, to get a more general picture:

plt.figure(figsize=(14,5))
plt.subplot(1,2,1)
p = np.random.random((1000,1))*100-50 # new random data in [-50, 50]
plt.plot(p,model.predict(p), '.')
plt.xlabel('x')
plt.ylabel('prediction')
plt.title('Predictions on NEW data in [-50,50]')

plt.subplot(1,2,2)
plt.xlabel('x')
plt.ylabel('y')
plt.plot(x,y,'.')
plt.title('Original data')

结果:

嗯,可以说确实确实是一个很好的近似值...

Well, it arguably does look like a good approximation indeed...

您也可以查看此线程以获取正弦近似值

You could also take a look at this thread for a sine approximation.

最后要记住的一点是,尽管即使使用相对简单的模型也可以得到不错的近似值,但应该期望的是外推,即[-50, 50]以外的良好表现;有关详细信息,请参见

The last thing to keep in mind is that, although we did get a decent approximation even with our relatively simple model, what we should not expect is extrapolation, i.e. good performance outside [-50, 50]; for details, see my answer in Is deep learning bad at fitting simple non linear functions outside training scope?

这篇关于平方(x ^ 2)逼近的神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆