Lecromine
Lecromine

Reputation: 178

Extracting neural net weights using tensorflow's tf.contrib.learn.DNNClassifier

Is there a way to extract weight matrices from Tensorflow's tf.contrib.learn.DNNClassifier? I've tried to look up the Tensorflow site for an answer but I'm fairly new to this so I haven't found anything helpful so far. Sorry in advance if there is already explicit explanation for this in here that I wasn't able to find.

My code:

# read the csv file to numpy array
df = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename="data.csv",
      target_dtype=np.int,
      features_dtype=np.float64)

X = df.data
Y = df.target
dimension = len(X)

feature_columns = [tf.contrib.layers.real_valued_column("", dimension=dimension)]

classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                                      hidden_units=[10,10],
                                                      n_classes=2,
                                                      activation_fn=tf.nn.sigmoid,
                                                      optimizer=tf.train.ProximalAdagradOptimizer(
                                                        learning_rate=0.1,
                                                        l2_regularization_strength=0.001))

#Fit model
classifier.fit(x=X, y=Y, steps=2000)

Upvotes: 1

Views: 1269

Answers (1)

Lecromine
Lecromine

Reputation: 178

After some research I think I've come up with the answer:

classifier.get_variable_value(classifier.get_variable_names()[3])   

classifier.get_variable_names() prints a list of names

['dnn/binary_logistic_head/dnn/learning_rate', 
'dnn/hiddenlayer_0/biases', 
'dnn/hiddenlayer_0/biases//hiddenlayer_0/biases/part_0/Adagrad',
'dnn/hiddenlayer_0/weights',
'dnn/hiddenlayer_0/weights/hiddenlayer_0/weights/part_0/Adagrad', 
'dnn/logits/biases', 
'dnn/logits/biases/dnn/dnn/logits/biases/part_0/Adagrad', 
'dnn/logits/weights', 
'dnn/logits/weights/nn/dnn/logits/weights/part_0/Adagrad',
'global_step']

And classifier.get_variable_names()[3] gets the fourth one, the weights for the first layer. The classifier in this case had one hidden layer with 10 neurons.

The 7th one 'dnn/logits/weights' gives the weights for the output layer.

Upvotes: 3

Related Questions