def fit(self, epochs, training_examples, training_targets, learning_rate):
        for epoch in range(epochs):
            cost = 0
            for x, y in zip(training_examples, training_targets):
                a, z = self.forward_propagate(x)
                self.back_propagate(a, y, z, learning_rate)
                cost += functions.mse(a[-1], y)

            mean_cost = cost / len(training_examples)
            print("Average cost on epoch " + str(epoch) + " : " +
                  str(mean_cost))
def testNetwork(network, testData):
    classes = testData[:, -1]
    features = testData[:, 0:-1]
    realOutput = []
    for inputs in features:
        output = network.feed(inputs)
        realOutput.append(np.argmax(output))

    realOutput = np.array(realOutput)
    mseResult = mse(realOutput, classes)
    hits = (realOutput == classes).mean()
    return mseResult, hits, realOutput
    print('<><><><><><><><><><><><><><><><><><><><><><><><><>')
    print("Opening Talos on window size: {}".format(i))
    dpl = file + '_deploy.zip'
    print(dpl)
    with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
        LSTM = ta.Restore(dpl)
    #y_pred = ANN.model.predict(x_test)
    #f = '/Users/mariusjessen/UiO19/MachineLearning/HW/FYS-STK-4155-Project3/my_dir/'+file + '.png'
    #plot_model(ANN.model, to_file=f,show_shapes=True)

    y_pred = data.inv.inverse_transform(LSTM.model.predict(x_test))
    y_test = data.inv.inverse_transform(y_test)

    #y_test, y_pred = y_test[:20], y_pred[:20]

    print('RMSE: {}'.format(np.sqrt(mse(y_test, y_pred))))
    print('MAPE: {}'.format(mean_absolute_percentage_error(y_test, y_pred)))
    print('AMAPE: {}'.format(AMAPE(y_pred, y_test)))
    print('MAE: {}'.format(MAE(y_pred, y_test)))
    print('PCT: {}'.format(PCT(y_test, y_pred)))

    rmse = np.sqrt(mse(y_test, y_pred))
    mape = mean_absolute_percentage_error(y_test, y_pred)
    amape = AMAPE(y_pred, y_test)
    mae = MAE(y_pred, y_test)
    pct = PCT(y_test, y_pred)
    res[ind, 0] = rmse
    res[ind, 1] = mape
    res[ind, 2] = pct
    res[ind, 3] = mae
    res[ind, 4] = amape
reconstruction[reconstruction < 0] = 0
reconstruction /= np.max(reconstruction)
reconstruction = np.round(reconstruction * 255).astype(np.uint8)

# use Otsu thresholding method to enhance blurring boundary
'''to solve: 2d image smooth to get rid of boundary oscillation, edge smooth? '''
enhanced_reconstruction = np.zeros_like(reconstruction)
val = filters.threshold_otsu(reconstruction)
mask = reconstruction < val
enhanced_reconstruction = 1 - mask.astype(int)

# validate reconstruction by calculating Correlation coefficient and Mean square error
cor = measure._structural_similarity.compare_ssim(enhanced_reconstruction,
                                                  phantom)
print('Correlation coefficient: ' + str(cor))
mse = func.mse(enhanced_reconstruction, phantom)
print('Mean square error: ' + str(mse))

# extract mesh from the volumeric image stack
verts_rec, faces_rec, normals_rec, values_rec = measure.marching_cubes_lewiner(
    enhanced_reconstruction,
    level=None,
    spacing=(1.0, 1.0, 1.0),
    gradient_direction='descent',
    step_size=6,
    allow_degenerate=True,
    use_classic=False)
verts_ori, faces_ori, normals_ori, values_ori = measure.marching_cubes_lewiner(
    phantom,
    level=None,
    spacing=(1.0, 1.0, 1.0),