import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
#データセットの読み込み・numpy配列に変換
h = (np.genfromtxt('data/h.csv', delimiter=',')).reshape(10000, 6, 2)
v = (np.genfromtxt('data/v.csv', delimiter=',')).reshape(10000, 6, 2)
#ラベルの作成・結合
h_labels = np.ones((10000, 1, 2))
v_labels = np.zeros((10000, 1, 2))
h = np.concatenate([h, h_labels], axis=1)
v = np.concatenate([v, v_labels], axis=1)
data = np.vstack((h, v))
#一応シャッフル
np.random.shuffle(data)
#訓練用とテスト用に分割
train, test = train_test_split(data, test_size=0.2)
#ターゲットデータとラベルの指定
train_x = train[:, :6, :]
train_y = train[:, -1, 1]
train_y = train_y.reshape(train_y.shape[0], 1)
test_x = test[:, :6, :]
test_y = test[:, -1, 1]
test_y = test_y.reshape(test_y.shape[0], 1)
#モデルの作成
model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation='relu',
kernel_initializer=tf.keras.initializers.HeNormal(),
input_shape=(6, 2)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer=Adam(learning_rate=0.001),
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(train_x, train_y, epochs=10, batch_size=32)
#モデルの評価
loss, accuracy = model.evaluate(test_x, test_y, verbose=0)
print('loss:', loss, 'accuracy:', accuracy)
#実際に水平・垂直のデータを予測できているかテスト
input1 = (np.genfromtxt('data/hpredict.csv', delimiter=',')).reshape(16, 6, 2)
input2 = (np.genfromtxt('data/vpredict.csv', delimiter=',')).reshape(16, 6, 2)
prediction1 = model.predict(input1)
prediction2 = model.predict(input2)
print(prediction1)
print(prediction2)