1919import tensorflow as tf
2020from tensorforce import TensorForceError
2121
22+
2223class MetaParameterRecorder (object ):
2324 """
2425 Class to record MetaParameters as well as Summary/Description for TensorBoard (TEXT & FILE will come later)
@@ -69,19 +70,31 @@ def __init__(self, current_frame):
6970
7071 def merge_custom (self , custom_dict ):
7172 if type (custom_dict ) is not dict :
72- raise TensorForceError ("Error: MetaParameterRecorder 'meta_dict' must be passed a dictionary but was passed a type {} which is not supported." .format (str (type (custom_dictdata ))))
73+ raise TensorForceError (
74+ "Error: MetaParameterRecorder 'meta_dict' must be passed a dictionary "
75+ "but was passed a type {} which is not supported." .format (str (type (custom_dict )))
76+ )
7377 for key in custom_dict :
7478 if key in self .meta_params :
75- raise TensorForceError ("Error: MetaParameterRecorder 'meta_dict' key {} conflicts with internal key, please change passed key." .format (str (key )))
79+ raise TensorForceError (
80+ "Error: MetaParameterRecorder 'meta_dict' key {} conflicts with internal key,"
81+ " please change passed key." .format (str (key ))
82+ )
7683 self .meta_params [key ] = custom_dict [key ]
7784 # This line assumes the merge data came from summary_spec['meta_dict'], remove this from summary_spec
7885 del self .meta_params ['summary_spec' ]['meta_dict' ]
7986
8087 def text_output (self , format_type = 1 ):
8188 print ('======================= ' + self .meta_params ['AgentName' ] + ' ====================================' )
8289 for key in self .meta_params :
83- print (" " , key , type (self .meta_params [key ]),"=" , self .convert_data_to_string (self .meta_params [key ],\
84- format_type = format_type ))
90+ print (
91+ " " ,
92+ key ,
93+ type (self .meta_params [key ]),
94+ "=" ,
95+ self .convert_data_to_string (self .meta_params [key ], format_type = format_type )
96+ )
97+
8598 print ('======================= ' + self .meta_params ['AgentName' ] + ' ====================================' )
8699
87100 def convert_dictionary_to_string (self , data , indent = 0 , format_type = 0 , seperator = None , eol = None ):
@@ -94,47 +107,57 @@ def convert_dictionary_to_string(self, data, indent=0, format_type=0, seperator=
94107
95108 #This should not ever occur but here as a catch
96109 if type (data ) is not dict :
97- raise TensorForceError ("Error: MetaParameterRecorder Dictionary conversion was passed a type {} not supported." .format (str (type (data ))))
98-
99- if format_type == 0 : # TensorBoard
100- label = ""
101- div = ""
110+ raise TensorForceError (
111+ "Error: MetaParameterRecorder Dictionary conversion was passed a type {}"
112+ " not supported." .format (str (type (data )))
113+ )
114+
115+ # TensorBoard
116+ if format_type == 0 :
117+ label = ""
118+ div = ""
119+
102120 if indent > 0 :
103- label = " | "
104- div = "--- | "
121+ label = " | "
122+ div = "--- | "
105123 data_string += label + "Key | Value" + eol + div + "--- | ----" + eol
106124
107125 for key in data :
108126 key_txt = key
109- if format_type == 0 : # TensorBoard
127+ # TensorBoard
128+ if format_type == 0 :
110129 key_txt = "**" + key + "**"
111130 key_value_sep = ' | '
112- if indent > 0 :
113- key_txt = " | " + key_txt
131+ if indent > 0 :
132+ key_txt = " | " + key_txt
114133
115- data_string += add_seperator + key_txt + key_value_sep + self .convert_data_to_string (data [key ],\
116- seperator = seperator , indent = indent + 1 ) + eol
134+ converted_data = self .convert_data_to_string (data [key ], seperator = seperator , indent = indent + 1 )
135+ data_string += add_seperator + key_txt + key_value_sep + converted_data + eol
117136
118137 return data_string
119138
120139 def convert_list_to_string (self , data , indent = 0 , format_type = 0 , eol = None , count = True ):
121- data_string = ""
140+ data_string = ""
122141 if eol is None :
123142 eol = os .linesep
124143
125144 #This should not ever occur but here as a catch
126145 if type (data ) is not list :
127- raise TensorForceError ("Error: MetaParameterRecorder List conversion was passed a type {} not supported." .format (str (type (data ))))
146+ raise TensorForceError (
147+ "Error: MetaParameterRecorder List conversion was passed a type {}"
148+ " not supported." .format (str (type (data )))
149+ )
128150
129151 for index ,line in enumerate (data ):
130152 data_string_prefix = ""
131153 if count and indent == 0 :
132154 data_string_prefix = str (index + 1 )+ ". "
133- if format_type == 0 : # TensorBoard
134- #Only add indent for 2nd item and beyond as this is likely a dictionary entry
135- if indent > 0 and index > 0 :
155+ # TensorBoard
156+ if format_type == 0 :
157+ # Only add indent for 2nd item and beyond as this is likely a dictionary entry
158+ if indent > 0 and index > 0 :
136159 data_string_prefix = " | " + data_string_prefix
137- if index == (len (data )- 1 ):
160+ if index == (len (data )- 1 ):
138161 append_eol = ""
139162 else :
140163 append_eol = eol
@@ -143,15 +166,18 @@ def convert_list_to_string(self, data, indent=0, format_type=0, eol=None, count=
143166 return data_string
144167
145168 def convert_ndarray_to_md (self , data , format_type = 0 , eol = None ):
146- data_string = ""
169+ data_string = ""
147170 data_string1 = "|Row|"
148171 data_string2 = "|:---:|"
149172 if eol is None :
150173 eol = os .linesep
151174
152175 #This should not ever occur but here as a catch
153176 if type (data ) is not np .ndarray :
154- raise TensorForceError ("Error: MetaParameterRecorder ndarray conversion was passed a type {} not supported." .format (str (type (data ))))
177+ raise TensorForceError (
178+ "Error: MetaParameterRecorder ndarray conversion was passed"
179+ " a type {} not supported." .format (str (type (data )))
180+ )
155181
156182 shape = data .shape
157183 rank = data .ndim
@@ -174,12 +200,12 @@ def convert_ndarray_to_md(self, data, format_type=0, eol=None):
174200 data_string += "|Row|Col-0|" + eol + "|:----:|:----:|" + eol
175201
176202 for row in range (shape [0 ]):
177- data_string += str (row ) + "|" + str (data [row ]) + "|" + eol
203+ data_string += str (row ) + "|" + str (data [row ]) + "|" + eol
178204
179205 return data_string
180206
181207 def convert_data_to_string (self , data , indent = 0 , format_type = 0 , seperator = None , eol = None ):
182- data_string = ""
208+ data_string = ""
183209 if type (data ) is int :
184210 data_string = str (data )
185211 elif type (data ) is float :
@@ -195,18 +221,21 @@ def convert_data_to_string(self, data, indent=0, format_type=0, seperator=None,
195221 elif type (data ) is dict :
196222 data_string = self .convert_dictionary_to_string (data , indent = indent , seperator = seperator )
197223 elif type (data ) is np .ndarray :
198- if format_type == 0 : # TensorBoard
224+ # TensorBoard
225+ if format_type == 0 :
199226 data_string = self .convert_ndarray_to_md (data )
200227 else :
201228 data_string = str (data )
202229 elif data is None :
203230 data_string = "None"
204231 else :
205232 if not self .ignore_unknown_dtypes :
206- data_string = "Error: MetaParameterRecorder Type conversion from type {} not supported." .format (str (type (data )))
233+ data_string = "Error: MetaParameterRecorder Type conversion from type {} not supported." .\
234+ format (str (type (data )))
207235 data_string += " (" + str (data )+ ") "
208236 else :
209- if format_type == 0 : # TensorBoard
237+ # TensorBoard
238+ if format_type == 0 :
210239 data_string = "**?**"
211240
212241 return data_string
@@ -239,5 +268,3 @@ def build_metagraph_list(self):
239268 self .summary_merged = tf .summary .merge_all ()
240269
241270 return self .summary_merged
242-
243-
0 commit comments