import os
import shutil

import numpy
import pandas
import tensorflow
from sklearn import metrics

ATTRIBUTES = ['source_node', 'destination_node', 'link_weight', ]
ATTRIBUTE_TYPES = [str, str, float, ]
FEATURE_ATTRIBUTES = ATTRIBUTES[:2]
TARGET_ATTRIBUTE = ATTRIBUTES[2]


def read_file(data_file):
    return pandas.read_csv(
        data_file,
        names=ATTRIBUTES,
        dtype={attribute_name: attribute_type for attribute_name, attribute_type in zip(ATTRIBUTES, ATTRIBUTE_TYPES)},
    )


def input_fn(links, num_epochs, shuffle):
    targets = links[TARGET_ATTRIBUTE]
    links = links.dropna(how='any', axis=0, )
    batch_size = 128
    return tensorflow.estimator.inputs.pandas_input_fn(links, targets, batch_size, num_epochs, shuffle)


def evaluate(model_dir, training_file, testing_file, dimension, num_epochs):
    training_set = read_file(training_file)
    testing_set = read_file(testing_file)
    categorical_columns = [
        tensorflow.feature_column.categorical_column_with_vocabulary_list(
            attribute, training_set[attribute].apply(str).unique(),
        ) for attribute in FEATURE_ATTRIBUTES
    ]
    embedding_columns = [
        tensorflow.feature_column.embedding_column(column, dimension) for column in categorical_columns
    ]
    model = tensorflow.estimator.DNNRegressor([dimension, dimension, ], embedding_columns, model_dir, )
    model.train(input_fn(training_set, num_epochs, True), )
    predictions = list(model.predict(input_fn(testing_set, 1, False)))
    actual_targets = numpy.concatenate([prediction['predictions'] for prediction in predictions])
    return metrics.mean_squared_error(actual_targets, testing_set[TARGET_ATTRIBUTE])


def main():
    for data_set_name in ['airport', 'collaboration', 'congress', 'forum', ]:
        for num_epochs in [4, 8, 16, 32, ]:
            for dimension in [4, 8, 16, 32, ]:
                model_dir = os.path.join('../log', data_set_name, str(num_epochs), str(dimension))
                if os.path.exists(model_dir):
                    shutil.rmtree(model_dir)
                print(
                    'data_set_name:', data_set_name,
                    'num_epochs:', num_epochs,
                    'dimension:', dimension,
                    evaluate(
                        model_dir,
                        os.path.join('../data', data_set_name + '_training.csv'),
                        os.path.join('../data', data_set_name + '_testing.csv'),
                        dimension,
                        num_epochs,
                    )
                )


if __name__ == '__main__':
    main()

@@ -623,29 +623,8 @@
 , )%0A
-    batch_size = 128%0A
     
@@ -694,20 +694,19 @@
 ts, 
-batch_size, 
+num_epochs=
 num_
@@ -720,16 +720,26 @@
  shuffle
+=shuffle, 
 )%0A%0A%0Adef 
@@ -747,25 +747,29 @@
 valuate(
-model_dir
+data_set_name
 , traini
@@ -812,18 +812,191 @@
 m_epochs
+, trial):%0A    model_dir = os.path.join('../log', data_set_name, str(num_epochs), str(dimension), str(trial))%0A    if os.path.exists(model_dir):%0A        shutil.rmtree(model_dir
 )
-:
 %0A    tra
@@ -1873,198 +1873,240 @@
     
-for data_set_name in %5B'airport', 'collaboration', 'congress', 'forum', %5D:%0A        for num_epochs in %5B4, 8, 16, 32, %5D:%0A            for dimension in %5B4, 8, 16, 32, %5D:%0A                model_dir
+errors = pandas.DataFrame(columns=%5B'data_set_name', 'num_epochs', 'dimension', 'error', %5D)%0A    # for data_set_name in %5B'airport', 'collaboration', 'congress', 'forum', %5D:%0A    for data_set_name in %5B'airport', %5D:%0A        training_file
  = o
@@ -2113,35 +2113,36 @@
 s.path.join('../
-log
+data
 ', data_set_name
@@ -2145,229 +2145,115 @@
 name
-, str(num_epochs), str(dimension))%0A                if os.path.exists(model_dir):%0A                    shutil.rmtree(model_dir)%0A                print(%0A                    'data_set_name:', data_set_name,%0A           
+ + '_training.csv')%0A        testing_file = os.path.join('../data', data_set_name + '_testing.csv')%0A
         
  'nu
@@ -2248,18 +2248,20 @@
         
+for
  
-'
 num_epoc
@@ -2266,25 +2266,26 @@
 ochs
-:', num_epochs,%0A 
+ in range(1, 11):%0A
     
@@ -2296,16 +2296,12 @@
     
-       '
+for 
 dime
@@ -2309,22 +2309,18 @@
 sion
-:', dimension,
+ in %5B4, %5D:
 %0A   
@@ -2336,21 +2336,28 @@
     
-    evaluate(
+error = numpy.mean(%5B
 %0A   
@@ -2377,22 +2377,17 @@
     
-    model_dir,
+evaluate(
 %0A   
@@ -2410,32 +2410,8 @@
     
- os.path.join('../data',
  dat
@@ -2416,37 +2416,34 @@
 ata_set_name
+,
  
-+ '_
 training
 .csv'),%0A    
@@ -2434,101 +2434,103 @@
 ning
-.csv'),%0A                        os.path.join('../data', data_set_name + '_testing.csv'),%0A    
+_file, testing_file, dimension, num_epochs, trial%0A                    ) for trial in range(10)%0A
     
@@ -2545,22 +2545,10 @@
     
-    dimension,
+%5D)
 %0A   
@@ -2564,66 +2564,96 @@
     
-        num_epochs,%0A                    )%0A                
+errors.loc%5Blen(errors)%5D = %5Bdata_set_name, num_epochs, dimension, error%5D%0A    print(errors
 )%0A%0A%0A

add grid search feature