zmeggyesi

training code

Oct 28th, 2020
753
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import pandas as pd
  3. import tensorflow as tf
  4. from sklearn.model_selection import train_test_split
  5.  
  6. from utils import df_to_dataset
  7. from utils import vectorizeDirections, createTemporalSignals, \
  8.     get_normalization_layer, create_category_crossing
  9.  
  10. tf.executing_eagerly()
  11. np.set_printoptions(precision=4)
  12. pd.set_option('display.max_columns', 500)
  13. pd.set_option('display.width', 5000)
  14. pd.set_option('display.max_colwidth', 500)
  15.  
  16. dataframe = pd.read_csv("./sample.csv", converters={'specimens': str})
  17.  
  18. dataframe = vectorizeDirections(dataframe)
  19. dataframe = createTemporalSignals(dataframe)
  20.  
  21. # plt.hist2d(dataframe['currentx'], dataframe['currenty'], bins=(50, 50), vmax=400)
  22. # plt.colorbar()
  23. # plt.xlabel('Current X [m/s]')
  24. # plt.ylabel('Current Y [m/s]')
  25. # ax = plt.gca()
  26. # ax.axis('tight')
  27. # plt.show()
  28.  
  29. train, test = train_test_split(dataframe, test_size=0.2)
  30. train, val = train_test_split(train, test_size=0.2)
  31. print(len(train), 'train examples')
  32. print(len(val), 'validation examples')
  33. print(len(test), 'test examples')
  34.  
  35. dataset = df_to_dataset(dataframe)
  36.  
  37. batch_size = 256
  38. train_ds = df_to_dataset(train, batch_size=batch_size)
  39. val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
  40. test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)
  41.  
  42. all_inputs = []
  43. encoded_features = []
  44.  
  45. # Numeric features.
  46. for header in ['current_x', 'current_y', 'air_temp', 'water_temp', 'waves_x', 'waves_y']:
  47.     numeric_col = tf.keras.Input(shape=(1,), name=header)
  48.     normalization_layer = get_normalization_layer(header, train_ds)
  49.     encoded_numeric_col = normalization_layer(numeric_col)
  50.     all_inputs.append(numeric_col)
  51.     encoded_features.append(encoded_numeric_col)
  52.  
  53. # Categorical features
  54. categorical_col = tf.keras.Input(name='timestamp', shape=(1,))
  55. encoding_layer = create_category_crossing('specimens', train_ds)
  56. all_inputs.append(categorical_col)
  57. encoded_features.append(encoding_layer)
  58.  
  59. all_features = tf.keras.layers.concatenate(encoded_features)
  60. x = tf.keras.layers.Dense(32, activation="relu")(all_features)
  61. x = tf.keras.layers.Dropout(0.5)(x)
  62. output = tf.keras.layers.Dense(1)(x)
  63. model = tf.keras.Model(all_inputs, output)
  64. model.compile(optimizer='adam',
  65.               loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
  66.               metrics=["accuracy"])
  67.  
  68. # rankdir='LR' is used to make the graph horizontal.
  69. tf.keras.utils.plot_model(model, show_shapes=True, rankdir="LR")
  70.  
  71.  
  72. # model.fit(train_ds, epochs=10, validation_data=val_ds)
  73. #
  74. # loss, accuracy = model.evaluate(test_ds)
  75. # print("Accuracy", accuracy)
  76.  
RAW Paste Data