t_max = 100  #total epochs

trainX, trainY, testX, testY = read_file("xi(1).csv", "tau(1).csv", P, Q)

model = Model(input_size=len(trainX[0]))

model.add_layer(states=2, activation='tanh', fixed_weights=False)
model.add_layer(states=1, activation=None, fixed_weights=1)

# model.display()

# print(trainX.shape)
initial_weights, final_weights = model.train(trainX,
                                             trainY,
                                             testX,
                                             testY,
                                             ephochs=t_max,
                                             learning_rate=learning_rate,
                                             return_weights=True,
                                             verbose=False)

# print(initial_weights)
# print("\n==========================================\n")

# print(final_weights)

for w in range(0, initial_weights.shape[0]):
    fig, axs = plt.subplots(2)
    axs[0].bar(np.arange(initial_weights[w].size), initial_weights[w])
    axs[0].set_title('Initial w_' + str(w) + 'weight vector')
    axs[0].set_ylabel('value')
Es = []
E_tests = []

for i in range(0, R):
    print("Run " + str(i + 1) + " of " + str(R))
    model = Model(input_size=len(trainX[0]))

    model.add_layer(states=2, activation='tanh', fixed_weights=False)
    model.add_layer(states=1, activation=None, fixed_weights=1)

    # model.display()

    # print(trainX.shape)
    E, E_test = model.train(trainX,
                            trainY,
                            testX,
                            testY,
                            ephochs=t_max,
                            learning_rate=learning_rate)
    Es.append(E)
    E_tests.append(E_test)

average_E = np.average(Es, axis=0)
average_Etest = np.average(E_tests, axis=0)

plt.plot(average_E, label="Empirical Error(E)")
plt.title("Error-Time Graph")

plt.plot(average_Etest, label="Validation Error(E_test)")

plt.xlabel('Time')
plt.ylabel('Averaged Error')
trainX, trainY, testX, testY = read_file("real world dataset/xzscore.csv",
                                         "real world dataset/tshift.csv",
                                         P,
                                         Q,
                                         shuffle=True)

# print(trainX.size)
model = Model(input_size=len(trainX[0]))

model.add_layer(states=2, activation='tanh', fixed_weights=False)
model.add_layer(states=1, activation=None, fixed_weights=1 / 4)

E, E_test = model.train(trainX,
                        trainY,
                        testX,
                        testY,
                        ephochs=t_max,
                        learning_rate=learning_rate,
                        verbose=False)

# model.display()
plt.plot(E, label="Empirical Error(E)")
plt.title("Error-Time Graph")

plt.plot(E_test, label="Validation Error(E_test)")

plt.xlabel('Time')
plt.ylabel('Error')

plt.legend()