gpt4 book ai didi

python - keras中的全梯度下降

转载 作者:太空狗 更新时间:2023-10-29 21:02:10 24 4
gpt4 key购买 nike

我正在尝试在 keras 中实现全梯度下降。这意味着对于每个时期,我都在整个数据集上进行训练。这就是批量大小定义为训练集长度大小的原因。

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD,Adam
from keras import regularizers
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import random
from numpy.random import seed
import random

def xrange(start_point,end_point,N,base):
temp = np.logspace(0.1, 1, N,base=base,endpoint=False)
temp=temp-temp.min()
temp=(0.0+temp)/(0.0+temp.max()) #this is between 0 and 1
return (end_point-start_point)*temp +start_point #this is the range

def train_model(x_train,y_train,x_test):
#seed(1)
model=Sequential()
num_units=100
act='relu'
model.add(Dense(num_units,input_shape=(1,),activation=act))
model.add(Dense(num_units,activation=act))
model.add(Dense(num_units,activation=act))
model.add(Dense(num_units,activation=act))
model.add(Dense(1,activation='tanh')) #output layer 1 unit ; activation='tanh'
model.compile(Adam(),'mean_squared_error',metrics=['mse'])
history=model.fit(x_train,y_train,batch_size=len(x_train),epochs=500,verbose=0,validation_split = 0.2 ) #train on the noise (not moshe)
fit=model.predict(x_test)
loss = history.history['loss']
val_loss = history.history['val_loss']
return fit

N = 1024
start_point=-5.25
end_point=5.25
base=500# the base of the log of the trainning
train_step=0.0007
x_test=np.arange(start_point,end_point,train_step+0.05)

x_train=xrange(start_point,end_point,N,base)
#random.shuffle(x_train)

function_y=np.sin(3*x_train)/2
noise=np.random.uniform(-0.2,0.2,len(function_y))
y_train=function_y+noise
fit=train_model(x_train,y_train,x_test)

plt.scatter(x_train,y_train, facecolors='none', edgecolors='g') #plt.plot(x_value,sample,'bo')
plt.scatter(x_test, fit, facecolors='none', edgecolors='b') #plt.plot(x_value,sample,'bo')

enter image description here

然而,当我取消注释 #random.shuffle(x_train) - 为了随机播放训练。 plot :

我不明白为什么我得到不同的情节(绿色圆圈是训练,蓝色是现代人学到的东西)。在这两种情况下,批处理都是所有数据集。所以洗牌不应该改变任何东西。
谢谢你 。

爱丽儿

最佳答案

发生这种情况有两个原因:

  • 首先,当数据没有打乱时,训练/验证拆分是不合适的。
  • 其次,完全梯度下降在每个时期执行一次更新,因此可能需要更多的训练时期才能收敛。

为什么你的模型不符合潮流?

来自 model.fit :

  • validation_split: Float between 0 and 1. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling.

这意味着您的验证集包含最后 20% 的训练样本。因为您对自变量 (x_train) 使用对数刻度,所以您的训练/验证拆分是:

split_point = int(0.2*N)
x_val = x_train[-split_point:]
y_val = y_train[-split_point:]
x_train_ = x_train[:-split_point]
y_train_ = y_train[:-split_point]
plt.scatter(x_train_, y_train_, c='g')
plt.scatter(x_val, y_val, c='r')
plt.show()

Train - validation split

在上图中,训练数据和验证数据分别由绿色和红色点表示。请注意,您的训练数据集代表整个人群。


为什么还是和训练数据集不匹配?

除了不适当的训练/测试拆分之外,完整梯度下降可能需要更多的训练时期才能收敛(梯度噪声较小,但它只执行单个每个时期的梯度更新)。相反,如果您训练模型约 1500 个时期(或使用批量大小为 32 的小批量梯度下降),您最终会得到:

Result

关于python - keras中的全梯度下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53769556/

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