tenowflow keras - model.predict, выдающий все те же результаты - PullRequest
0 голосов
/ 18 марта 2020
  1. модель. предсказываем, что получаю все те же выходные данные
  2. Получение низкой проверки и точности тестирования <60% </li>

Я пытался изменить последний скрытый слой на softmax, но он все еще не решил эту проблему. Любая обратная связь будет оценена. Я также попытался поиграть с гиперпараметрами, но все равно не смог найти никакого решения.

 raw_cvs_data = np.loadtxt('data_to_train.csv',delimiter=',')
 raw_cvs_data_to_compute = np.loadtxt('data_to_compute.csv',delimiter=',')

 unscaled_inputs_all = raw_cvs_data[:,1:]
 targets_all = raw_cvs_data[:,0]
 inputs_to_compute = raw_cvs_data_to_compute[:]
 predicted_target=[]

 # balancing the dataset
 num_one_targets = int(np.sum(targets_all)) # count how many targets are 1
 zero_targets_counter = 0 # counter for target 0

 indices_to_remove = [] # remove extra input/target pairs for balance 

 # count the number of targets 0, when get same amount of target 1 and 0, make entries where target is zero
 for i in range(targets_all.shape[0]):
         if targets_all[i] == 0:
             zero_targets_counter +=1
             if zero_targets_counter > num_one_targets:
                 indices_to_remove.append(i)

 unscaled_inputs_equal_priors = np.delete(unscaled_inputs_all,indices_to_remove, axis = 0)

 targets_equal_priors = np.delete(targets_all, indices_to_remove, axis = 0)
 #Shuffle the data 
 shuffled_indices = np.arange(scaled_inputs.shape[0])
 np.random.shuffle(shuffled_indices) #shuffle pairs

 shuffled_inputs = scaled_inputs[shuffled_indices]
 shuffled_targets = targets_equal_priors[shuffled_indices]
 # splitting data
 samples_count = shuffled_inputs.shape[0]

 # |training|validation|testing| 80-10-10
 train_samples_count = int(0.8 * samples_count)
 validation_samples_count = int(0.1 *samples_count)
 test_samples_count = samples_count - train_samples_count - validation_samples_count

 train_inputs = shuffled_inputs[:train_samples_count]
 train_targets = shuffled_targets[:train_samples_count]

 validation_inputs = shuffled_inputs[train_samples_count:train_samples_count + validation_samples_count]
 validation_targets = shuffled_targets[train_samples_count:train_samples_count + validation_samples_count]

 test_inputs = shuffled_inputs[train_samples_count + validation_samples_count:]
 test_targets = shuffled_targets[train_samples_count + validation_samples_count:]
 np.savez('training_data', inputs=train_inputs, targets=train_targets)
 np.savez('validation_data', inputs=validation_inputs, targets=validation_targets)
 np.savez('test_data', inputs=test_inputs, targets=test_targets)
 np.savez('data_to_compute', inputs=inputs_to_compute)


 #model.py-


 input_size = 22
 output_size = 2 # 0 / 1
 hidden_layer_size = 50 # width

 model = tf.keras.Sequential([tf.keras.layers.Dense(output_size, activation='sigmoid'),tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
 tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
 tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
 tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
 tf.keras.layers.Dense(output_size, activation='sigmoid')])

 model.compile(optimizer='sgd', loss='mean_squared_error',metrics=['accuracy'])
 max_epochs = 500
 model.fit(train_inputs, 
           train_targets, 
           batch_size=1,
           epochs=max_epochs, 
           callbacks = [early_stopping],
           validation_data=(validation_inputs, validation_targets),
           validation_steps=10,
           verbose=2
          )
 prediction = model.predict(data_to_compute)

[[0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
      [0.09466213 0.09515946]
     .
     .
     .
     .
     .]```


Ответы [ 2 ]

0 голосов
/ 18 марта 2020

Если, как вам кажется, вы пытаетесь выполнить классификацию с метками, закодированными в горячем коде, то у вашего кода есть две проблемы.

Во-первых, вы используете неправильный код потеря; среднеквадратическая ошибка (MSE) используется для регрессии задач, а не для классификации. Измените компиляцию вашей модели, чтобы использовать двоичную кросс-энтропийную потерю, т.е. 1011 *

tf.keras.layers.Dense(output_size, activation='softmax')

, который является правильным для ярлыков с горячим кодированием.

0 голосов
/ 18 марта 2020

Ваша проблема связана с этой строкой кода: tf.keras.layers.Dense(output_size, activation='sigmoid').

Проблема в том, что вы используете активацию 'sigmoid' с 2 нейронами вместо 1.

Либо используйте 2 neurons + activation = 'softmax' или 1 neuron + activation='sigmoid'.

...