Machine learning Dropout Tensorflow:测试时的重量缩放
我是否需要在tensorflow中在测试时缩放重量,即重量*在测试时保持?或者tensorflow自己做?如果是的话,那怎么办? 在训练中,我的keep_prob为0.5。在测试时,它是1。 虽然对网络进行了正则化处理,但精度不如正则化前。 另外,我正在给CIFAR10分类Machine learning Dropout Tensorflow:测试时的重量缩放,machine-learning,tensorflow,neural-network,computer-vision,deep-learning,Machine Learning,Tensorflow,Neural Network,Computer Vision,Deep Learning,我是否需要在tensorflow中在测试时缩放重量,即重量*在测试时保持?或者tensorflow自己做?如果是的话,那怎么办? 在训练中,我的keep_prob为0.5。在测试时,它是1。 虽然对网络进行了正则化处理,但精度不如正则化前。 另外,我正在给CIFAR10分类 n_nodes_h1=1000 n_nodes_h2=1000 n_nodes_h3=400 n_nodes_h4=100 classes=10 x=tf.placeholder('float',[None,3073])
n_nodes_h1=1000
n_nodes_h2=1000
n_nodes_h3=400
n_nodes_h4=100
classes=10
x=tf.placeholder('float',[None,3073])
y=tf.placeholder('float')
keep_prob=tf.placeholder('tf.float32')
batch_size=100
def neural_net(data):
hidden_layer1= {'weight':tf.Variable(tf.random_normal([3073,n_nodes_h1])),
'biases':tf.Variable(tf.random_normal([n_nodes_h1]))}
hidden_layer2={'weight':tf.Variable(tf.random_normal([n_nodes_h1,n_nodes_h2])),
'biases':tf.Variable(tf.random_normal([n_nodes_h2]))}
out_layer={'weight':tf.Variable(tf.random_normal([n_nodes_h2,classes])),
'biases':tf.Variable(tf.random_normal([classes]))}
l1= tf.add(tf.matmul(data,hidden_layer1['weight']), hidden_layer1['biases'])
l1=tf.nn.relu(l1)
#************DROPOUT*******************
l1=tf.nn.dropout(l1,keep_prob)
l2= tf.add(tf.matmul(l1,hidden_layer2['weight']), hidden_layer2['biases'])
l2=tf.nn.relu(l2)
out= tf.matmul(l2,out_layer['weight'])+ out_layer['biases']
return out
这是网络
iterations=20
Train_loss=[]
Test_loss=[]
def train_nn(x):
prediction=neural_net(x)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
optimizer=tf.train.AdamOptimizer().minimize(cost)
epochs=iterations
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range (epochs):
e_loss=0
i=0
for _ in range(int(X_train.shape[0]/batch_size)):
e_x=X_train[i:i+batch_size]
e_y=y_hot_train[i:i+batch_size]
i+=batch_size
_,c=sess.run([optimizer,cost],feed_dict={x:e_x,y:e_y, keep_prob:0.5})
e_loss+=c
print "Epoch: ",epoch," Train loss= ",e_loss
Train_loss.append(e_loss)
correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct,'float'))
print "Accuracy on test: " ,accuracy.eval({x:X_test,y:y_hot_test , keep_prob:1.})
print "Accuracy on train:" ,accuracy.eval({x:X_train[0:2600],y:y_hot_train[0:2600], keep_prob=1.})
train_nn(x)
我需要像这样的东西吗
hidden_layer1['weight']*=keep_prob
#testing time
Tensorflow本身就是这样做的: 使用概率keep_prob,输出按1放大的输入元素/ 保持_prob,否则输出0。缩放是为了达到预期的效果 金额不变
(from)Tensorflow本身: 使用概率keep_prob,输出按1放大的输入元素/ 保持_prob,否则输出0。缩放是为了达到预期的效果 金额不变
(from)不,你不需要。退出层将其所有输出乘以
1/keep_prob
。所以我的网络看起来很好?您的退出实现很好。但是你的人脉不够深,不适合辍学。增加你的网络复杂度,当训练数据过多时,增加辍学率以使其正常化。不,你不需要这样做。退出层将其所有输出乘以1/keep_prob
。所以我的网络看起来很好?您的退出实现很好。但是你的人脉不够深,不适合辍学。增加您的网络复杂性,当训练数据过多时,添加退出以使其正常化。