我正在使用Keras进行预测。我使用两个数字阵列训练它,Y1和Y2用于每个输入数字X.
我想预测_Y1和_Y2,但我不知道如何。
可视化显示什么都没有。那讲得通。检查它,它看起来像"预测"数组是空的。
import numpy as np
import pandas
import math
import random
from keras.models import Sequential, Model
from keras.layers import Input, Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
import graphviz
import pydot
import matplotlib.pyplot as plt
X = np.array([])
Y_int = np.array([])
Y1 = np.array([])
Y2 = np.array([])
count = -1
while count < 1:
count += 0.001
X += np.array([count])
i = ( math.sin(count) )
Y_int += np.array([i])
if i > 1 or i < -1:
o = 1
u = 1 / i
else:
o = i
u = 1
Y1 += np.array([o])
Y2 += np.array([u])
length = len(X)
# define base model
def baseline_model():
# create model
model = Sequential()
inp = Input((1,))
x = Dense(100, kernel_initializer='lecun_normal', activation='tanh')(inp)
out1 = Dense(1, kernel_initializer='lecun_normal')(x)
out2 = Dense(1, kernel_initializer='lecun_normal')(x)
model = Model(inp, [out1,out2])
# Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model
estimator = baseline_model();
estimator.fit(X, [Y1, Y2], batch_size = 20, epochs = 1, verbose = 0)
plt.axis([-1, 1, -1, 1])
plt.ion()
while True:
estimator.fit(X, [Y1, Y2], batch_size = 20, epochs = 1, verbose = 0)
prediction = estimator.predict(X)
plt.clf()
plt.plot(X, Y_int)
plt.plot(X, prediction )
plt.pause(0.001)
答案 0 :(得分:1)
+=
不是附加numpy数组的正确方法(我认为这是你的目的);这是您的第一个while
循环的结果:
count = -1
while count < 1:
count += 0.001
X += np.array([count])
i = ( math.sin(count) )
Y_int += np.array([i])
if i > 1 or i < -1:
o = 1
u = 1 / i
else:
o = i
u = 1
Y1 += np.array([o])
Y2 += np.array([u])
length = len(X)
length
# 0
X
# array([], dtype=float64)
Y1
# array([], dtype=float64)
Y2
# array([], dtype=float64)
即。你的所有阵列都是空的......
如果追加数组确实是你的目的,请使用numpy.append
方法:
count = -1
while count < 1:
count += 0.001
X = np.append(X, np.array([count]))
i = math.sin(count))
Y_int = np.append(Y_int, np.array([i]))
if i > 1 or i < -1:
o = 1
u = 1 / i
else:
o = i
u = 1
Y1 = np.append(Y1, np.array([o]))
Y2 = np.append(Y2, np.array([u]))
length = len(X)
length
# 2000
len(Y1), len(Y2), len(Y_int)
# (2000, 2000, 2000)
此外,您应该从模型定义中删除model = Sequential()
行,因为您显然正在使用Keras Functional API。