fix: use correct order for prediction

This commit is contained in:
Mahdi Dibaiee 2019-03-05 15:23:29 +03:30
parent 3dcafddb8c
commit 8477c02aae
4 changed files with 22 additions and 13 deletions

Binary file not shown.

View File

@ -39,7 +39,7 @@ def draw(df, path=None):
} }
for n in biome_numbers: for n in biome_numbers:
biomes[n] = MultiPoint(biomes[n]).buffer(1) biomes[n] = MultiPoint(biomes[n]).buffer(0.5)
# print(biomes[n]) # print(biomes[n])
# legend = biome_names[n] # legend = biome_names[n]
if not hasattr(biomes[n], '__iter__'): if not hasattr(biomes[n], '__iter__'):

24
nn.py
View File

@ -55,18 +55,22 @@ class Model():
params = { params = {
'kernel_initializer': 'lecun_uniform', 'kernel_initializer': 'lecun_uniform',
'bias_initializer': 'zeros', 'bias_initializer': 'zeros',
# 'kernel_regularizer': keras.regularizers.l2(l=0.01)
} }
# dropout = keras.layersDropout(0.2, input_shape=[self.features]) dropout = [keras.layers.Dropout(0.1, input_shape=[self.features])]
self.model = keras.Sequential([ self.model = keras.Sequential(dropout + [
keras.layers.Dense(layers[0], activation=tf.nn.elu, input_shape=[self.features], **params) keras.layers.Dense(layers[0], activation=tf.nn.elu, **params)
] + [ ] + [
keras.layers.Dense(n, activation=tf.nn.elu, **params) for n in layers[1:] keras.layers.Dense(n, activation=tf.nn.elu, **params) for n in layers[1:]
] + [ ] + [
keras.layers.Dense(self.output_size, activation=out_activation, **params) keras.layers.Dense(self.output_size, activation=out_activation, **params)
]) ])
def compile(self, loss='mse', metrics=['accuracy'], optimizer=tf.train.AdamOptimizer): def compile(self, loss='mse', metrics=['accuracy'], optimizer=tf.train.AdamOptimizer, load_weights=True):
self.model.load_weights(self.path) if load_weights:
self.model.load_weights(self.path)
print('loaded weights')
optimizer = optimizer(self.learning_rate) optimizer = optimizer(self.learning_rate)
self.model.compile(loss=loss, self.model.compile(loss=loss,
@ -81,12 +85,16 @@ class Model():
verbose=1 verbose=1
) )
def evaluate_print(self):
loss, accuracy = self.evaluate()
print('Test evaluation: loss: {}, accuracy: {}'.format(loss, accuracy))
def train(self): def train(self):
self.model.summary() self.model.summary()
checkpoint = keras.callbacks.ModelCheckpoint(self.path, monitor='acc', verbose=1, mode='max') checkpoint = keras.callbacks.ModelCheckpoint(self.path, monitor='acc', verbose=1, mode='max')
tensorboard = keras.callbacks.TensorBoard(log_dir='./logs', update_freq='epoch') tensorboard = keras.callbacks.TensorBoard(log_dir='./logs', update_freq='epoch')
# map_callback = keras.callbacks.LambdaCallback(on_epoch_end=self.map_callback) map_callback = keras.callbacks.LambdaCallback(on_epoch_end=self.evaluate_print)
self.model.fit( self.model.fit(
self.training, self.training,
@ -101,11 +109,11 @@ class Model():
return np.argmax(self.model.predict(a), axis=1) return np.argmax(self.model.predict(a), axis=1)
A = Model('a', epochs=2) A = Model('a', epochs=2)
B = Model('b', learning_rate=0.001, epochs=450) B = Model('b', learning_rate=0.001, epochs=20)
def compile_b(): def compile_b():
B.prepare_dataset(df, dataframe_to_dataset_biomes) B.prepare_dataset(df, dataframe_to_dataset_biomes)
B.create_model([32], tf.nn.softmax) B.create_model([32, 32], tf.nn.softmax)
B.compile(loss='sparse_categorical_crossentropy') B.compile(loss='sparse_categorical_crossentropy')
def compile_a(): def compile_a():

View File

@ -17,7 +17,7 @@ compile_b()
for change in range(0, 1): for change in range(0, 1):
print('TEMPERATURE MODIFICATION OF {}'.format(change)) print('TEMPERATURE MODIFICATION OF {}'.format(change))
inputs = ['latitude', 'longitude', 'elevation', 'distance_to_water'] inputs = ['elevation', 'distance_to_water', 'latitude']
for season in SEASONS: for season in SEASONS:
inputs += [ inputs += [
@ -25,8 +25,10 @@ for change in range(0, 1):
'precip_{}_{}'.format(season, year) 'precip_{}_{}'.format(season, year)
] ]
print(inputs)
# print(inputs) # print(inputs)
frame = df[inputs] frame = df[inputs + ['longitude']]
# print(frame.head()) # print(frame.head())
for season in SEASONS: for season in SEASONS:
@ -38,7 +40,7 @@ for change in range(0, 1):
for i, chunk in enumerate(chunker(frame, B.batch_size)): for i, chunk in enumerate(chunker(frame, B.batch_size)):
if chunk.shape[0] < B.batch_size: if chunk.shape[0] < B.batch_size:
continue continue
input_data = normalize_ndarray(chunk.loc[:, chunk.columns != 'longitude'].values) input_data = normalize_ndarray(chunk.loc[:, inputs].values)
out = B.predict(input_data) out = B.predict(input_data)
f = pd.DataFrame({ f = pd.DataFrame({
@ -48,5 +50,4 @@ for change in range(0, 1):
}, columns=columns) }, columns=columns)
new_data = new_data.append(f) new_data = new_data.append(f)
print(new_data)
draw(new_data) draw(new_data)