美文网首页
Tensorflow最佳工程实践——抽象与简化

Tensorflow最佳工程实践——抽象与简化

作者: 卜是 | 来源:发表于2020-08-13 21:03 被阅读0次

    虽然在原生的Tensorflow中,提供了颗粒度较小的接口,方便我们灵活创建和训练深度学习模型,但若不做任何的抽象化或封装,将导致代码显得繁琐冗余。为了增强代码的可读性,需要对原始的tensorflow接口组合包装成函数或类,从而有助于代码的重复利用以及缩短开发周期。
    本文通过Tensorflow中一些实用性扩展,来实现对Tensorflow的抽象和简化,而无需自己来编写相关抽象层,掌握这些扩展库的使用,可显著提升开发效率。

    1、tf.contrib.learn

    熟悉scikit-learn机器学习库的开发人员可很容易使用该库,对于简单的模型,可达到“即插即用”的程度。

    1. 实例化一个估计器来创建模型
    model = learn.<some_estimator>()
    
    1. 加载数据
    tf.contrib.learn.datasets.base.load_csv_with_header
    
    1. 训练模型
    model.fit()
    
    1. 评估模型
    model.evaluate()
    
    1. 预测结果
    model.predict()
    
    1. 实际案例
      DNN分类器
    import sys
    import numpy as np
    from tensorflow.examples.tutorials.mnist import input_data
    DATA_DIR = '/tmp/data' if not 'win32' in sys.platform else "c:\\tmp\\data"
    data = input_data.read_data_sets(DATA_DIR, one_hot=False)
    x_data, y_data = data.train.images,data.train.labels.astype(np.int32)
    x_test, y_test = data.test.images,data.test.labels.astype(np.int32)
    NUM_STEPS = 2000
    MINIBATCH_SIZE = 128
    
    feature_columns = learn.infer_real_valued_columns_from_input(x_data)
    dnn = learn.DNNClassifier(
        feature_columns=feature_columns,
        hidden_units=[200],
        n_classes=10,
        optimizer=tf.train.ProximalAdagradOptimizer(
        learning_rate=0.2)
        )
    
    dnn.fit(x=x_data,y=y_data, steps=NUM_STEPS,
            batch_size=MINIBATCH_SIZE)
    
    test_acc = dnn.evaluate(x=x_test,y=y_test, steps=1)["accuracy"]
    print('test accuracy: {}'.format(test_acc))
    

    2、tf.contrib.layers

    def model_fn(x, target, mode, params):
        y_ = tf.cast(target, tf.float32)
        x_image = tf.reshape(x, [-1, 28, 28, 1])
    
        # Conv layer 1
        conv1 = layers.convolution2d(x_image, 32, [5,5],
                    activation_fn=tf.nn.relu,
                    biases_initializer=tf.constant_initializer(0.1),
                    weights_initializer=tf.truncated_normal_initializer(stddev=0.1))
        pool1 = layers.max_pool2d(conv1, [2,2])
    
        # Conv layer 2
        conv2 = layers.convolution2d(pool1, 64, [5,5],
                    activation_fn=tf.nn.relu,
                    biases_initializer=tf.constant_initializer(0.1),
                    weights_initializer=tf.truncated_normal_initializer(stddev=0.1))
        pool2 = layers.max_pool2d(conv2, [2,2])
    
        # FC layer
        pool2_flat = tf.reshape(pool2, [-1, 7*7*64])
        fc1 = layers.fully_connected(pool2_flat, 1024,
                  activation_fn=tf.nn.relu,
                  biases_initializer=tf.constant_initializer(0.1),
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.1))
        fc1_drop = layers.dropout(fc1, keep_prob=params["dropout"],
            is_training=(mode == 'train'))
    
        # readout layer
        y_conv = layers.fully_connected(fc1_drop, 10, activation_fn=None)
    
        cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y_))
        train_op = tf.contrib.layers.optimize_loss(
            loss=cross_entropy,
            global_step=tf.contrib.framework.get_global_step(),
            learning_rate=params["learning_rate"],
            optimizer="Adam")
    
        predictions = tf.argmax(y_conv, 1)
        return predictions, cross_entropy, train_op
    
    from tensorflow.contrib import layers
    data = input_data.read_data_sets(DATA_DIR, one_hot=True)
    x_data, y_data = data.train.images,np.int32(data.train.labels)
    tf.cast(x_data,tf.float32)
    tf.cast(y_data,tf.float32)
    model_params = {"learning_rate": 1e-4, "dropout": 0.5}
    
    # 创建自己的估计器,也就是对应的模型
    CNN = tf.contrib.learn.Estimator(
        model_fn=model_fn, params=model_params)
    
    print("Starting training for %s steps max" % 5000)
    CNN.fit(x=data.train.images,
            y=data.train.labels, batch_size=50,
            max_steps=5000)
    
    test_acc = 0
    for ii in range(5):
        batch = data.test.next_batch(2000)
        predictions = list(CNN.predict(batch[0], as_iterable=True))
        test_acc = test_acc + (np.argmax(batch[1],1) == predictions).mean()
    
    print(test_acc/5)
    

    3、TFLearn

    用它创建一个自定义模型的过程更为简洁

    import tflearn
    from tflearn.layers.core import input_data, dropout, fully_connected
    from tflearn.layers.conv import conv_2d, max_pool_2d
    from tflearn.layers.normalization import local_response_normalization
    from tflearn.layers.estimator import regression
    
    # Data loading and basic trasformations
    import tflearn.datasets.mnist as mnist
    X, Y, X_test, Y_test = mnist.load_data(one_hot=True)
    X = X.reshape([-1, 28, 28, 1])
    X_test = X_test.reshape([-1, 28, 28, 1])
    
    # Building the network
    CNN = input_data(shape=[None, 28, 28, 1], name='input')
    CNN = conv_2d(CNN, 32, 5, activation='relu', regularizer="L2")
    CNN = max_pool_2d(CNN, 2)
    CNN = local_response_normalization(CNN)
    CNN = conv_2d(CNN, 64, 5, activation='relu', regularizer="L2")
    CNN = max_pool_2d(CNN, 2)
    CNN = local_response_normalization(CNN)
    CNN = fully_connected(CNN, 1024, activation=None)
    CNN = dropout(CNN, 0.5)
    CNN = fully_connected(CNN, 10, activation='softmax')
    CNN = regression(CNN, optimizer='adam', learning_rate=0.0001,
                         loss='categorical_crossentropy', name='target')
    
    # Training the network
    model = tflearn.DNN(CNN,tensorboard_verbose=0,tensorboard_dir = 'MNIST_tflearn_board/',\
                        checkpoint_path = 'MNIST_tflearn_checkpoints/checkpoint')
    model.fit({'input': X}, {'target': Y}, n_epoch=3, 
               validation_set=({'input': X_test}, {'target': Y_test}),
               snapshot_step=1000,show_metric=True, run_id='convnet_mnist')
    
    evaluation = model.evaluate({'input': X_test},{'target': Y_test})
    print(evaluation)
    
    pred = model.predict({'input': X_test})
    print((np.argmax(testY,1)==np.argmax(pred,1)).mean())
    

    使用tflearn实现RNN模型的训练

    from tflearn.data_utils import to_categorical, pad_sequences
    from tflearn.datasets import imdb
    
    # Load data
    train, test, _ = imdb.load_data(path='imdb.pkl', n_words=10000,
                                    valid_portion=0.1)
    X_train, Y_train = train
    X_test, Y_test = test
    
    # Sequence padding and Converting labels to binary vectors
    X_train = pad_sequences(X_train, maxlen=100, value=0.)
    X_test = pad_sequences(X_test, maxlen=100, value=0.)
    Y_train = to_categorical(Y_train, nb_classes=2)
    Y_test = to_categorical(Y_test, nb_classes=2)
    
    # Building a LSTM network
    RNN = tflearn.input_data([None, 100])
    RNN = tflearn.embedding(RNN, input_dim=10000, output_dim=128)
    
    RNN = tflearn.lstm(RNN, 128, dropout=0.8)
    RNN = tflearn.fully_connected(RNN, 2, activation='softmax')
    RNN = tflearn.regression(RNN, optimizer='adam', learning_rate=0.001,
                             loss='categorical_crossentropy')
    
    # Training the network
    model = tflearn.DNN(RNN, tensorboard_verbose=0)
    model.fit(X_train, Y_train, validation_set=(X_test, Y_test),
                                    show_metric=True, batch_size=32)
    

    4、Keras

    相关文章

      网友评论

          本文标题:Tensorflow最佳工程实践——抽象与简化

          本文链接:https://www.haomeiwen.com/subject/vqwbdktx.html