tensorflow的keras實現搭配dataset 之二


tensorflow的keras實現搭配dataset,幾種形式都工作!

討論 tensorflow的keras 函數式,而不去討論原生keras的,因為原生的keras的與dataset的搭配不好!

定義函數模型的方式有兩種,其中一種能讓原生的keras與dataset很好工作,另一種不能;本文討論

tensorflow的keras與dataset花式搭配,感覺好自由哦!

 

 

from tensorflow import keras as ks
import tensorflow as tf

# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20)).astype(np.float32)
y_train = ks.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10).astype(np.float32)
x_test = np.random.random((100, 20)).astype(np.float32)
y_test = ks.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10).astype(np.float32)


batch_size = 100
steps_per_epoch = int(np.ceil(x_train.shape[0]/batch_size))

train_ds = tf.data.Dataset.from_tensor_slices((x_train,y_train))
train_ds = train_ds.batch(batch_size).repeat()   # batch 能給數據集增加批維度
train_it = train_ds.make_one_shot_iterator()
x_train_it, y_train_it = train_it.get_next()


test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))
test_ds = test_ds.batch(batch_size).repeat()
test_it = train_ds.make_one_shot_iterator()
x_test_it, y_test_it = test_it.get_next()


def gen_model1():
    model_input = ks.layers.Input(shape=(20,))
    x = ks.layers.Dense(64, activation='relu')(model_input)
    x = ks.layers.Dropout(0.5)(x)
    x = ks.layers.Dense(64, activation='relu')(x)
    x = ks.layers.Dropout(0.5)(x)
    model_output = ks.layers.Dense(10, activation='softmax')(x)
    train_model = tf.keras.models.Model(inputs=model_input, outputs=model_output)
    sgd = ks.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    train_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
    train_model.summary()
    return train_model

# passing the data to the model with the below to style, both work
model = gen_model1()
model.fit(x_train_it, y_train_it, epochs=20, steps_per_epoch=steps_per_epoch)
score = model.evaluate(test_ds, steps=128)
print(score)
print("(+("*20,'\n'*4)
model.fit(train_ds, epochs=20, steps_per_epoch=steps_per_epoch)

score = model.evaluate(test_ds, steps=128)
print(score)

print("\n"*6)

def gen_model2(inputs, targets):
    model_input = ks.layers.Input(tensor=inputs)
    x = ks.layers.Dense(64, activation='relu')(model_input)
    x = ks.layers.Dropout(0.5)(x)
    x = ks.layers.Dense(64, activation='relu')(x)
    x = ks.layers.Dropout(0.5)(x)
    model_output = ks.layers.Dense(10, activation='softmax')(x)
    train_model = tf.keras.models.Model(inputs=model_input, outputs=model_output)
    sgd = ks.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    train_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'],target_tensors=[targets])
    train_model.summary()
    return train_model


# passing the data to the model with the below to style, both work
model = gen_model2(x_train_it, y_train_it)
model.fit(epochs=20, steps_per_epoch=steps_per_epoch)
score = model.evaluate(test_ds, steps=128)
print(score)
score = model.evaluate(x_test_it, y_test_it, steps=128)
print(score)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM