2019-02-13:超参数调优

2019-02-13  本文已影响0人  AI_Finance

import numpyas np

import tensorflowas tf

from hyperoptimport fmin, tpe, hp

def data_generator():

while True:

a1 = np.random.rand(10, 2)

factors = np.array([[2], [3]])

yield a1, np.matrix(a1)*np.matrix(factors)

dg = data_generator()

class function_fitness(object):

def __init__(self, sess, learning_rate=1e-2):

self.sess = sess

self.learn_rate = learning_rate

self.build_placeholder()

def build_placeholder(self):

self.input_x = tf.placeholder(dtype=tf.float32, shape=[10, 2], name="input_x")

self.input_y = tf.placeholder(dtype=tf.float32, shape=[10, 1], name="input_y")

self.y_hat = tf.layers.dense(inputs=self.input_x, units=2,

                                    kernel_initializer=tf.truncated_normal_initializer(),

                                    bias_initializer=tf.truncated_normal_initializer())

self.loss = tf.reduce_mean(input_tensor=tf.square(x=self.input_y -self.y_hat))

self.train_op = tf.train.AdamOptimizer(learning_rate=self.learn_rate).minimize(loss=self.loss)

def train(self, learning_rate):

self.learn_rate = learning_rate

x, y = dg.__next__()

_, self.loss_ =self.sess.run(fetches=[self.train_op, self.loss], feed_dict={self.input_x: x, self.input_y: y})

print(self.loss_, "\t", self.learn_rate)

return self.loss_

with tf.Session()as sess:

ff = function_fitness(sess=sess)

sess.run(tf.global_variables_initializer())

best = fmin(fn=ff.train, space=hp.uniform('learning_rate', 0, 1),

                algo=tpe.suggest,

                max_evals=8000)

print(best)

上一篇下一篇

猜你喜欢

热点阅读