1. 程式人生 > >keras上手之:與tensorflow混合程式設計

keras上手之:與tensorflow混合程式設計

tensorflow具備許多優秀的函式和功能,比如tensorboard,keras作為tensorflow的高階API, 封裝很多tensorflow的程式碼,使得程式碼模組化,非常方便。
當然,由於keras的模型和層與tensorflow的張量高度相容,可以用keras建模,用tensorflow輸入輸出。
例如下面的例子:

import tensorflow as tf
from keras import backend as K
from keras.layers import Dense
from keras.objectives import categorical_crossentropy
from
keras.metrics import categorical_accuracy as accuracy from tensorflow.examples.tutorials.mnist import input_data # create a tf session,and register with keras。 sess = tf.Session() K.set_session(sess) # this place holder is the same with input layer in keras img = tf.placeholder(tf.float32, shape=(None, 784
)) # keras layers can be called on tensorflow tensors x = Dense(128, activation='relu')(img) x = Dense(128, activation='relu')(x) preds = Dense(10, activation='softmax')(x) # label labels = tf.placeholder(tf.float32, shape=(None, 10)) # loss function loss = tf.reduce_mean(categorical_crossentropy(labels, preds)
) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss) mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True) # initialize all variables init_op = tf.global_variables_initializer() sess.run(init_op) with sess.as_default():
for i in range(1000): batch = mnist_data.train.next_batch(50) train_step.run(feed_dict={img:batch[0], labels:batch[1]}) acc_value = accuracy(labels, preds) with sess.as_default(): print(acc_value.eval(feed_dict={img:mnist_data.test.images, labels:mnist_data.test.labels}))

上述程式碼中,在訓練階段直接採用了tf的方式,甚至都沒有定義keras的model!最重要的一步就是這裡:

# create a tf sessionand register with keras。
sess = tf.Session()
K.set_session(sess)

建立一個TensorFlow會話並且註冊Keras。這意味著Keras將使用我們註冊的會話來初始化它在內部建立的所有變數。
keras的層和模型都充分相容tensorflow的各種scope, 例如name scope,device scope和graph scope。修改一下,在tensorboard輸出訓練過程中的loss曲線:

import tensorflow as tf
from keras import backend as K
from keras.layers import Dense
from keras.objectives import categorical_crossentropy
from keras.metrics import categorical_accuracy as accuracy
from tensorflow.examples.tutorials.mnist import input_data

sess = tf.Session()
K.set_session(sess)

with tf.name_scope('input'):
    # this place holder is the same with input layer in keras
    img = tf.placeholder(tf.float32, shape=(None, 784))
    labels = tf.placeholder(tf.float32, shape=(None, 10))

mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)
def feed_dict(train):
    if train:
        xs, ys = mnist_data.train.next_batch(50)
    else:
        xs, ys = mnist_data.test.images, mnist_data.test.labels
    return {img:xs, labels:ys}


# keras layers can be called on tensorflow tensors
with tf.name_scope('NN'):
    x = Dense(128, activation='relu')(img)
    x = Dense(128, activation='relu')(x)
    preds = Dense(10, activation='softmax')(x)

with tf.name_scope('loss'):
    loss = tf.reduce_mean(categorical_crossentropy(labels, preds))
# tensorboard
writer = tf.summary.FileWriter('./keras_tensorflow_log/')
outloss = tf.summary.scalar('loss', loss)
merged = tf.summary.merge([outloss])

with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

# initialize all variables
init_op = tf.global_variables_initializer()
sess.run(init_op)

with sess.as_default():
    for i in range(1000):
        summary, loss = sess.run([merged, train_step], 
                 feed_dict=feed_dict(True))
        writer.add_summary(summary, global_step=i)
writer.close()  

在命令列輸入:
tensorboard --logdir=./keras_tensorflow_log
開啟tensorboard就可以看到loss history了:
這裡寫圖片描述