Scritto ed eseguito sul portatile con Windows 10 – Effetto South Working
Utilizzo l’environment conda py3_tf
1
| ~$ conda activate py3_tf
|
TensorBoard
Basato Breast cancer Wisconsin costruiamo i log per tenere traccia
1
2
3
4
5
6
7
8
9
10
11
12
13
| import pandas as pd
import numpy as np
from datetime import datetime
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation,Dropout
from tensorflow.keras.callbacks import EarlyStopping,TensorBoard
|
1
2
| # df
df = pd.read_csv('cancer_classification.csv')
|
1
2
3
| # X e y
X = df.drop('benign_0__mal_1',axis=1).values
y = df['benign_0__mal_1'].values
|
1
2
| # train e test
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.25,random_state=101)
|
1
2
3
4
5
| # scaling
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
|
1
2
| # early stopping
early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=25)
|
1
2
| # working directory
os.getcwd()
|
1
| 'F:\\Python\\Course 001'
|
1
2
| # time
datetime.now().strftime("%Y-%m-%d--%H%M")
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| # definizione tensorboard
# WINDOWS: Use "logs\\fit"
# MACOS/LINUX: Use "logs\fit"
log_directory = 'logs\\fit'
# OPTIONAL: ADD A TIMESTAMP FOR UNIQUE FOLDER
timestamp = datetime.now().strftime("%Y-%m-%d--%H%M")
log_directory = log_directory + '\\' + timestamp
board = TensorBoard(
log_dir=log_directory,
histogram_freq=1, # dopo ciascuna epoch calcola i pesi
write_graph=True,
write_images=True,
update_freq='epoch',
profile_batch=2,
embeddings_freq=1)
|
1
2
3
4
5
6
7
8
| # definizione modello
model = Sequential()
model.add(Dense(units=30,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=15,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=1,activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
|
1
2
3
4
5
6
7
| # training modello (lanciarlo solo una volta nella stessa callback log_dir)
model.fit(x=X_train,
y=y_train,
epochs=600,
validation_data=(X_test, y_test), verbose=0,
callbacks=[early_stop,board]
)
|
1
2
3
| Epoch 00138: early stopping
<tensorflow.python.keras.callbacks.History at 0x2a852471d08>
|
Monitoring
Attivo tensorboard dal terminale
1
| tensorboard --logdir logs\fit
|
Con il servizio attivo posso visitare http://localhost:6006/
Questo servizio di controllo e diagnostica è più utile per le convolutional neural networks.
1
| logs\fit\2020-08-01--1701
|