【问题标题】:What is the difference between the .trainable and training parameters in TensorflowTensorflow中的.trainable和training参数有什么区别
【发布时间】:2022-12-24 02:04:57
【问题描述】:

model.trainable=Falsemodel(..,training=False) 有什么区别?一般来说,什么时候一个比另一个使用,什么时候它们在一个模型中一起使用?

【问题讨论】:

    标签: tensorflow tensorflow2.0


    【解决方案1】:

    trainable 是张量的属性,表示该张量是否可以在训练期间由您的优化器更新。

    training 是一个旗帜通知被调用的层/模型在训练期间进行了前向调用。这是必要的,因为某些层在训练和推理期间表现不同,并且此标志用于其__call__() 方法中的某些切换逻辑。一个值得注意的例子是batch normalization层。

    您完全可以拥有一个具有非trainable 权重的层,但行为会有所不同,具体取决于它是否在training 期间被调用。

    【讨论】:

    • 事实上,最后一段的一个很好的例子就是 dropout。没有参数,但行为不同。
    【解决方案2】:

    这在执行之间是可能的称呼模型或预测他们这样做是因为在训练和称呼.

    您可以阅读他们准备的内容,并解释了有关权重参数、层和可调用方法(包括学习转移)的内容。 Learning transfer

    [ 样本 ]:

    import os
    from os.path import exists
    
    import tensorflow as tf
    import tensorflow_io as tfio
    
    import matplotlib.pyplot as plt
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
    None
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    physical_devices = tf.config.experimental.list_physical_devices('GPU')
    assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
    config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
    print(physical_devices)
    print(config)
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Variables
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    PATH = os.path.join('F:\datasets\downloads\Actors\train\Pikaploy', '*.tif')
    PATH_2 = os.path.join('F:\datasets\downloads\Actors\train\Candidt Kibt', '*.tif')
    files = tf.data.Dataset.list_files(PATH)
    files_2 = tf.data.Dataset.list_files(PATH_2)
    
    list_file = []
    list_file_actual = []
    list_label = []
    list_label_actual = [ 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Pikaploy', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt', 'Candidt Kibt' ]
    for file in files.take(5):
        image = tf.io.read_file( file )
        image = tfio.experimental.image.decode_tiff(image, index=0)
        list_file_actual.append(image)
        image = tf.image.resize(image, [32,32], method='nearest')
        list_file.append(image)
        list_label.append(1)
        
    for file in files_2.take(5):
        image = tf.io.read_file( file )
        image = tfio.experimental.image.decode_tiff(image, index=0)
        list_file_actual.append(image)
        image = tf.image.resize(image, [32,32], method='nearest')
        list_file.append(image)
        list_label.append(9)
    
    checkpoint_path = "F:\models\checkpoint\" + os.path.basename(__file__).split('.')[0] + "\TF_DataSets_01.h5"
    checkpoint_dir = os.path.dirname(checkpoint_path)
    loggings = "F:\models\checkpoint\" + os.path.basename(__file__).split('.')[0] + "\loggings.log"
    
    if not exists(checkpoint_dir) : 
        os.mkdir(checkpoint_dir)
        print("Create directory: " + checkpoint_dir)
        
    log_dir = checkpoint_dir
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    DataSet
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    dataset = tf.data.Dataset.from_tensor_slices((tf.constant(tf.cast(list_file, dtype=tf.int64), shape=(10, 1, 32, 32, 4), dtype=tf.int64),tf.constant(list_label, shape=(10, 1, 1), dtype=tf.int64)))
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Callback
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    class custom_callback(tf.keras.callbacks.Callback):
        def on_epoch_end(self, epoch, logs={}):
            if( logs['accuracy'] >= 0.97 ):
                self.model.stop_training = True
        
    custom_callback = custom_callback()
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Model Initialize
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    model = tf.keras.models.Sequential([
        tf.keras.layers.InputLayer(input_shape=( 32, 32, 4 )),
        tf.keras.layers.Normalization(mean=3., variance=2.),
        tf.keras.layers.Normalization(mean=4., variance=6.),
        # tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
        # tf.keras.layers.MaxPooling2D((2, 2)),
        # tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Reshape((128, 32)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(192, activation='relu'),
        tf.keras.layers.Dense(10),
    ])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Optimizer
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    optimizer = tf.keras.optimizers.Nadam( learning_rate=0.000001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, name='Nadam' )
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Loss Fn
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
    # lossfn = tf.keras.losses.MeanSquaredLogarithmicError(reduction=tf.keras.losses.Reduction.AUTO, name='mean_squared_logarithmic_error')
    lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=False,
        reduction=tf.keras.losses.Reduction.AUTO,
        name='sparse_categorical_crossentropy'
    )
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Model Summary
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Training
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    history = model.fit( dataset, batch_size=100, epochs=10000, callbacks=[custom_callback] )
    model.save_weights(checkpoint_path)
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Transfer learning
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    for layer in model.layers[:-1]:
        layer.trainable = False
    
    model_transferred = tf.keras.models.Sequential([
        model,
        tf.keras.layers.Dense(128),
        tf.keras.layers.Dense(10),
    ])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Model Summary ( 2 )
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    model_transferred.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
    
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    : Training ( 2 )
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    history = model_transferred.fit( dataset, batch_size=100, epochs=10000, callbacks=[custom_callback] )
    
    plt.figure(figsize=(6, 6))
    plt.title("Actors recognitions")
    for i in range(len(list_file)):
        img = tf.keras.preprocessing.image.array_to_img(
            list_file[i],
            data_format=None,
            scale=True
        )
        img_array = tf.keras.preprocessing.image.img_to_array(img)
        img_array = tf.expand_dims(img_array, 0)
        predictions = model_transferred.predict(img_array)
        score = tf.nn.softmax(predictions[0])
        plt.subplot(6, 6, i + 1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(list_file_actual[i])
        plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" +  str(list_label_actual[tf.math.argmax(score)]))
        
    plt.show()
    
    input('*.*')
    

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 1970-01-01
      • 2020-03-16
      • 2010-09-14
      • 1970-01-01
      • 2016-11-15
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多