Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How long have CMA-ES cost on training? #3

Open
yrj88351 opened this issue Aug 20, 2018 · 2 comments
Open

How long have CMA-ES cost on training? #3

yrj88351 opened this issue Aug 20, 2018 · 2 comments

Comments

@yrj88351
Copy link

How long have CMA-ES cost on training?
I wanna use tensorflow to test the CMA-ES on MNIST, as the same networks as yours, the training time become slower as the iteration increase.....
And the loss_val (cross-entropy) and accuracy have no obvious change after 50 iteration....(it have cost about 5 hours)
I use the same hyper-parameter as your setting...(population:101,sigma:0.01)

@yrj88351
Copy link
Author

yrj88351 commented Aug 20, 2018

import os
import numpy as np
import cma
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets('./MNIST',
one_hot=True)

os.environ['CUDA_VISIBLE_DEVICES'] = '2'
config = tf.ConfigProto()
config.gpu_options.allow_growth = True

sess=tf.Session(config=config)

def cal_loss(par):

batch_xs,batch_ys=mnist.train.next_batch(2000)
conv_1=np.array(par[0:200])
conv_1=np.reshape(conv_1,[5,5,1,8])
conv_b1=np.array(par[200:208])
conv_2=np.array(par[208:3408])
conv_2=np.reshape(conv_2,[5,5,8,16])
conv_b2=np.array(par[3408:3424])
w1=np.array(par[3424:11264])
w1=np.reshape(w1,[784,10])
b1=np.array(par[11264:11274])

with sess.as_default():
    
    x=tf.placeholder(dtype=tf.float32,shape=[None,784])
    x_image=tf.reshape(x,[-1,28,28,1])
    y_=tf.placeholder(dtype=tf.float32,shape=[None,10])
    
    kernel_1=tf.placeholder(dtype=tf.float32,shape=[5,5,1,8])
    kernel_b1=tf.placeholder(dtype=tf.float32,shape=[8])

    kernel_2=tf.placeholder(dtype=tf.float32,shape=[5,5,8,16])
    kernel_b2=tf.placeholder(dtype=tf.float32,shape=[16])

    fc_w1=tf.placeholder(dtype=tf.float32,shape=[7*7*16,10])
    fc_b1=tf.placeholder(dtype=tf.float32,shape=[10])
    
    conv1=tf.nn.conv2d(x_image,kernel_1,strides=[1,1,1,1],padding='SAME')
    output_conv1=tf.nn.relu(tf.nn.bias_add(conv1,kernel_b1))
    output_pool1=tf.nn.max_pool(output_conv1,ksize=[1,2,2,1],strides=[1,2,2,1],
                                 padding='SAME')
    
    conv2=tf.nn.conv2d(output_pool1,kernel_2,strides=[1,1,1,1],padding='SAME')
    output_conv2=tf.nn.relu(tf.nn.bias_add(conv2,kernel_b2))
    output_pool2=tf.nn.max_pool(output_conv2,ksize=[1,2,2,1],strides=[1,2,2,1],
                                padding='SAME')
    
    output_pool2_flatten=tf.reshape(output_pool2,[-1,7*7*16])
    y=tf.nn.softmax(tf.matmul(output_pool2_flatten,fc_w1)+fc_b1)

    cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y+1e-8),reduction_indices=[1]))
    
    init=tf.global_variables_initializer()
    sess.run(init)
    
    loss=sess.run(cross_entropy,feed_dict={
                                   kernel_1:conv_1,
                                   kernel_b1:conv_b1,
                                   kernel_2:conv_2,
                                   kernel_b2:conv_b2,
                                   fc_w1:w1,
                                   fc_b1:b1,
                                   x:(batch_xs),
                                   y_:(batch_ys)
                                   })
    
    return loss

def cal_accuracy(par):

batch_xs=mnist.test.images
batch_ys=mnist.test.labels
conv_1=np.array(par[0:200])
conv_1=np.reshape(conv_1,[5,5,1,8])
conv_b1=np.array(par[200:208])
conv_2=np.array(par[208:3408])
conv_2=np.reshape(conv_2,[5,5,8,16])
conv_b2=np.array(par[3408:3424])
w1=np.array(par[3424:11264])
w1=np.reshape(w1,[784,10])
b1=np.array(par[11264:11274])

with sess.as_default():    
    
    x=tf.placeholder(dtype=tf.float32,shape=[None,784])
    x_image=tf.reshape(x,[-1,28,28,1])
    y_=tf.placeholder(dtype=tf.float32,shape=[None,10])
    
    kernel_1=tf.placeholder(dtype=tf.float32,shape=[5,5,1,8])
    kernel_b1=tf.placeholder(dtype=tf.float32,shape=[8])

    kernel_2=tf.placeholder(dtype=tf.float32,shape=[5,5,8,16])
    kernel_b2=tf.placeholder(dtype=tf.float32,shape=[16])

    fc_w1=tf.placeholder(dtype=tf.float32,shape=[7*7*16,10])
    fc_b1=tf.placeholder(dtype=tf.float32,shape=[10])
    
    conv1=tf.nn.conv2d(x_image,kernel_1,strides=[1,1,1,1],padding='SAME')
    output_conv1=tf.nn.relu(tf.nn.bias_add(conv1,kernel_b1))
    output_pool1=tf.nn.max_pool(output_conv1,ksize=[1,2,2,1],strides=[1,2,2,1],
                                 padding='SAME')
    
    conv2=tf.nn.conv2d(output_pool1,kernel_2,strides=[1,1,1,1],padding='SAME')
    output_conv2=tf.nn.relu(tf.nn.bias_add(conv2,kernel_b2))
    output_pool2=tf.nn.max_pool(output_conv2,ksize=[1,2,2,1],strides=[1,2,2,1],
                                padding='SAME')
    
    output_pool2_flatten=tf.reshape(output_pool2,[-1,7*7*16])
    y=tf.nn.softmax(tf.matmul(output_pool2_flatten,fc_w1)+fc_b1)
    
    correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
    accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    
    init=tf.global_variables_initializer()
    sess.run(init)
    
    loss=sess.run(accuracy,feed_dict={
                                   kernel_1:conv_1,
                                   kernel_b1:conv_b1,
                                   kernel_2:conv_2,
                                   kernel_b2:conv_b2,
                                   fc_w1:w1,
                                   fc_b1:b1,
                                   x:(batch_xs),
                                   y_:(batch_ys)
                                   })
    
    return loss

def main():

es = cma.CMAEvolutionStrategy(11274 * [0.0],0.01,{'verb_disp':1,'popsize':101})
while not es.stop():
    solutions = es.ask()
    print(np.array(solutions).shape)
    es.tell(solutions, [cal_loss(x) for x in solutions])
    print('loss:',end='')
    print(es.result[1])
    print('accuracy:',end='')
    print(cal_accuracy(es.result[0]))
    es.disp()
    if es.result[1]<0.8:
        break

es.result_pretty()

if name == 'main':
main()

@yrj88351
Copy link
Author

yrj88351 commented Aug 20, 2018

I put my code there....
I think i really need your advice ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant