我正在尝试使用多层神经网络来预测第n个方格.
我有以下训练数据,包含前99个方格
1 1
2 4
3 9
4 16
5 25
...
98 9604
99 9801
Run Code Online (Sandbox Code Playgroud)
这是代码:
import numpy as np
import neurolab as nl
# Load input data
text = np.loadtxt('data_sq.txt')
# Separate it into datapoints and labels
data = text[:, :1]
labels = text[:, 1:]
# Define a multilayer neural network with 2 hidden layers;
# First hidden layer consists of 10 neurons
# Second hidden layer consists of 6 neurons
# Output layer consists of 1 …Run Code Online (Sandbox Code Playgroud) artificial-intelligence machine-learning neural-network python-3.x tensorflow
我是TensorFlow和数据科学的新手。我做了一个简单的模块,应该弄清楚输入和输出数字之间的关系。在这种情况下,x和x平方。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 = …Run Code Online (Sandbox Code Playgroud) 我刚开始学习keras.我正在尝试在keras中训练一个非线性回归模型,但模型似乎并没有学到太多东西.
#datapoints
X = np.arange(0.0, 5.0, 0.1, dtype='float32').reshape(-1,1)
y = 5 * np.power(X,2) + np.power(np.random.randn(50).reshape(-1,1),3)
#model
model = Sequential()
model.add(Dense(50, activation='relu', input_dim=1))
model.add(Dense(30, activation='relu', init='uniform'))
model.add(Dense(output_dim=1, activation='linear'))
#training
sgd = SGD(lr=0.1);
model.compile(loss='mse', optimizer=sgd, metrics=['accuracy'])
model.fit(X, y, nb_epoch=1000)
#predictions
predictions = model.predict(X)
#plot
plt.scatter(X, y,edgecolors='g')
plt.plot(X, predictions,'r')
plt.legend([ 'Predictated Y' ,'Actual Y'])
plt.show()
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
在花费数天未能使用神经网络进行 Q 学习之后,我决定回归基础并做一个简单的函数近似,看看一切是否正常工作,以及一些参数如何影响学习过程。这是我想出的代码
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
import random
import numpy
from sklearn.preprocessing import MinMaxScaler
regressor = Sequential()
regressor.add(Dense(units=20, activation='sigmoid', kernel_initializer='uniform', input_dim=1))
regressor.add(Dense(units=20, activation='sigmoid', kernel_initializer='uniform'))
regressor.add(Dense(units=20, activation='sigmoid', kernel_initializer='uniform'))
regressor.add(Dense(units=1))
regressor.compile(loss='mean_squared_error', optimizer='sgd')
#regressor = ExtraTreesRegressor()
N = 5000
X = numpy.empty((N,))
Y = numpy.empty((N,))
for i in range(N):
X[i] = random.uniform(-10, 10)
X = numpy.sort(X).reshape(-1, 1)
for i in range(N):
Y[i] = numpy.sin(X[i])
Y = Y.reshape(-1, 1)
X_scaler = MinMaxScaler()
Y_scaler = MinMaxScaler() …Run Code Online (Sandbox Code Playgroud)