- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在 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')
然而,当我取消注释 #random.shuffle(x_train) - 为了随机播放训练。 :
我不明白为什么我得到不同的情节(绿色圆圈是训练,蓝色是现代人学到的东西)。在这两种情况下,批处理都是所有数据集。所以洗牌不应该改变任何东西。
谢谢你 。
爱丽儿
最佳答案
发生这种情况有两个原因:
来自 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()
在上图中,训练数据和验证数据分别由绿色和红色点表示。请注意,您的训练数据集不代表整个人群。
除了不适当的训练/测试拆分之外,完整梯度下降可能需要更多的训练时期才能收敛(梯度噪声较小,但它只执行单个每个时期的梯度更新)。相反,如果您训练模型约 1500 个时期(或使用批量大小为 32 的小批量梯度下降),您最终会得到:
关于python - keras中的全梯度下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53769556/
我想循环遍历 gpx 文件并计算总上升和下降。我有一个函数可以计算两组经纬度点之间的高程差异,我已经设置了 simplexml 来读取和循环遍历 gpx 文件 trkseg 点。 问题是,这不准确(实
我有两个在不同时间段拍摄的数组。如何通过将新玩家标记为上升来检查哪些玩家在列表中上升/下降? 附言- 数组已经根据分数排序。 pastData:[ { playerName:'Jo
我想捕获 ctrl/alt/etc 键的起伏,无论表单上的哪个控件获取 keyup 或 keydown 事件。由于我的表单上有大约 100 个控件,如果我要为每个单独的控件添加代码,那将非常难看。我怎
vector1 = c(2, 2, 2, 2, 2, 2) vector2 = c(2, 2, 3, 3, 3, 3) vector3 = c(2, 2, 1, 2, 2, 2) 我想知道向量中的数字
我不知道如何遵循编译器的建议:consider using a let binding to create a longer lived value。 Playground #![allow(unus
我希望有人能帮助我理解 AngularJS 中的 $scope 遇到的一个恼人的问题。请参阅下面我的代码中的注释: app.controller('MyController', function ($
我有一个 flex 搜索集群,其中有2个节点在2核CPU 8GB ram实例上运行。每个节点都传入了参数“ES_JAVA_OPTS = -Xms3g -Xmx3g”。我有4个索引,每个索引有2个分片和
我正在学习 R(及其通过 quantmod lib 在交易任务中的应用)并定期浏览社区以从这里获得许多新知识和技巧。我对 R 的总体印象和特别是 quantmod lib 的印象 - 它很棒。 在这一
当我们点击屏幕时,我正在绘制纹理正方形。我正在使用相同的纹理。在新 ios 设备中点击几次后,FPS 从 120 下降到 4 左右。每次手指点击时,我都会将点击的点以及纹理和纹理的大小传递给着色器。
只有当对象被点击并且需要从列表中移除时它才会掉落。这是代码: if(event.type == TouchEvent.TOUCH_DOWN){ for(Bottle bottl
我有一个基于SpriteKit的小游戏。 在这个游戏中,我使用了很多带有字母(或字母组合)的节点,用户可以四处移动来构建单词。 这些节点基本上是带有 SKLabelNode 的 SKSpriteNod
我有一个简单的CSS布局 wrapper header left-sidebar / main-content / right-sidebar footer 但我的主要内容似乎下降了(float dr
在标题中,我给出了四个不同的部分,并使用 float 属性使所有内容都显示在一条水平线上。 当我调整浏览器窗口大小时,最后一个 div 位于黑色边框线下方。 如何解决。 http://jsfiddle
CSS: .desc{ text-align: center; color:#60A8D5; padding-top: 17px;
这是一段简单的代码,但我为这个问题尝试过的解决方案都没有奏效。 #ONE { float: left; border: 1
我有一个 SceneKit 设置,其中有一个 Sphere 设置为 Dynamic body。 我能够运行该应用程序并看到球体落在静态 body 地板上。 我想做的是设置场景,这样 sfere 最初就
首先,我的类(class): export class FooBar { ... isFavorite: boolean = false; constructor() { this.isF
我正在尝试删除所有端口上的所有传出 RST 和传入 RST。我正在使用 Debian Linux。我尝试了互联网上列出的所有可能的命令组合,但似乎没有任何效果。 例如,我试过: iptables -A
我正在做这样的事情: fn main() { //[1, 0, 0, 0, 99]; // return [2, 0, 0, 0, 99] //[2, 3, 0, 3, 99]; //
我正在使用 Rusqlite,它可以让你做这样的查询: statement.query_row(params!([1, 2, 3]), ...); params!()定义如下: macro_rules
我是一名优秀的程序员,十分优秀!