gpt4 book ai didi

python - Keras 2d 散乱预测

转载 作者:行者123 更新时间:2023-11-30 08:48:11 26 4
gpt4 key购买 nike

Keras 预测在各处返回相同的值。

我有一些 xyz 数据,我想使用 keras ML 在常规网格中进行预测。我使用了错误的东西并且无法弄清楚。

import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import Adadelta, Adam

m=1e5
data=np.random.rand(m,3) # let's generate some random data (i do actually have real data that make sense)


dx=0.05
xmin=np.min(data[:,0])
xmax=np.max(data[:,0])
ymin=np.min(data[:,1])
ymax=np.max(data[:,1])

xs=np.arange(xmin,xmax+dx,dx)
ys=np.arange(ymin,ymax+dx,dx)

xg,yg=np.meshgrid(xs,ys)

shape = (len(ys), len(xs))



activation='sigmoid'

hidden_layer_sizes=[128, 64, 32, 16]

keras_model = Sequential()

keras_model.add(Dense(hidden_layer_sizes[0], activation=activation, input_shape=(2, )))

for hl_size in hidden_layer_sizes[1: ]:
keras_model.add(Dense(hl_size, activation=activation))

keras_model.add(Dense(1))
keras_model.compile(loss='mean_squared_error', optimizer=Adam())
keras_model.save_weights('cache.h5')
keras_model.summary()



keras_model.load_weights('cache.h5') # re-initialize Keras model weights
keras_history = keras_model.fit(data[:,:2], data[:,2], batch_size=m, epochs=20000, verbose=1)


X_test = np.vstack((xg.flatten(), yg.flatten())).T

res_keras=keras_model.predict(X_test).reshape(shape)

我期望一些值“接近”插值函数。我的代码哪里有错误?

最佳答案

激活sigmoid更改为relu

设置

activation='relu'

关于python - Keras 2d 散乱预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56785632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com