How to see the weights in Tensorflow?

Asked

Viewed 111 times

4

I made a neural network to estimate a final value for myself, based on 7 inputs. It has only one hidden_layer with 95 perceptrons. I’d like to see what weights are used after training. I’ve searched to use tools like Tf.Print() and Tf.GraphKeys.TRAINABLE_VARIABLES, but I can’t see. Would anyone like to help me with this? The basis of the code I’m using is below.

def norm(x, train_stats):
  return (x - train_stats['mean']) / train_stats['std']

def build_model(qtd, act, train_dataset):
  model = keras.Sequential([
    layers.Dense(qtd, activation=act, input_shape=[len(train_dataset.keys())]),
    #layers.Dense(50, activation=tf.nn.sigmoid),
    layers.Dense(1)
  ])

  optimizer = tf.keras.optimizers.RMSprop(0.001)

  model.compile(loss='mean_squared_error',
                optimizer=optimizer,
                metrics=['mean_absolute_error', 'mean_squared_error'])
  return model

def neural(dataset):
    # Split of train and test data
    train_dataset = dataset[:len(dataset) - 2]
    test_dataset = dataset.drop(train_dataset.index)

    # Data statistics
    train_stats = train_dataset.describe()
    train_stats.pop("Pta Taquara")
    train_stats = train_stats.transpose()

    train_labels = train_dataset.pop('Pta Taquara')
    test_labels = test_dataset.pop('Pta Taquara')

    normed_train_data = norm(train_dataset, train_stats)
    normed_test_data = norm(test_dataset, train_stats)

    EPOCHS = 1000

    # The patience parameter is the amount of epochs to check for improvement
    early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)

    #qtds = [10, 50, 100, 500, 1000]
    qtds = [95]

    #acts = [tf.nn.elu, tf.nn.relu, tf.nn.selu, tf.nn.sigmoid, tf.nn.tanh]
    acts = [tf.nn.tanh]

    for a in acts:
        for q in qtds:
            model = build_model(q, a, train_dataset)

            history = model.fit(normed_train_data, train_labels, epochs=EPOCHS,
                                validation_split = 0.2, verbose=0, callbacks=[early_stop, PrintDot()])

            hist = pd.DataFrame(history.history)

            hist['epoch'] = history.epoch

            loss, mae, mse = model.evaluate(normed_test_data, test_labels, verbose=0)

            print("\nTesting set Mean Abs Error: {:5.2f} Pta Taquara".format(mae))

            test_predictions = model.predict(normed_test_data).flatten()
            print(test_predictions)
  • Already took a look at the tensorboard?

1 answer

1

Two pieces of information to answer your question:

  1. An object of the class model has a property layers, that returns a list of all layers. You can use it to access the layers individually. For example:
# acessa a primeira camada:
model.layers[0]

# percorre as camadas uma a uma e imprime o nome de cada camada no console:
for layer in model.layers:
    print(layer.name)
  1. An object of the class layer has a method get_weights(), which returns the weights of that layer. So, what you want to check can be seen with the following code:
# retorna os pesos e biases da primeira camada:
model.layers[0].get_weights()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.