-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
51 lines (36 loc) · 1.21 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import tempfile
import tensorflow as tf
import pandas as pd
TRAIN_DATASET = "train.csv"
TARGET_NAME = "target"
BUFFER_SIZE = 1000
BATCH_SIZE = 32
EPOCHS = 10
MODEL_DIR = tempfile.gettempdir() # Directory where the trained model will be exported
MODEL_VERSION = "1"
def load_data():
df = pd.read_csv(TRAIN_DATASET)
target = df.pop(TARGET_NAME)
dataset = tf.data.Dataset.from_tensor_slices((df, target))
batches = dataset.shuffle(buffer_size=BUFFER_SIZE).batch(batch_size=BATCH_SIZE)
return batches
def build_model():
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation="relu"),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation="relu"),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
return model
if __name__ == '__main__':
batches = load_data()
model = build_model()
model.summary()
model.fit(batches, epochs=EPOCHS, batch_size=BATCH_SIZE)
export_path = os.path.join(MODEL_DIR, MODEL_VERSION)
model.export(export_path)