I want to get the weights of each node of every layer in the DNNClassifier, trained using the estimator API of tensorflow. I found that it is possible to get weights of each node in keras. Is it possible for estimator API? Thanks for your help.
input_func = tf.estimator.inputs.pandas_input_fn(x=X_train,y=y_train,batch_size=10,num_epochs=1000,shuffle=True)
dnn_model = tf.estimator.DNNClassifier(hidden_units=[10,10,10],feature_columns=feat_cols,n_classes=2
model.train(input_fn,steps=6000)
I have used the above code to train the model. I want to further extract the weights of each node of hidden layer.
Yes, it should be possible to do so. You can extract the trainable variables names with:
train_var_names = [var.name for var in tf.trainable_variables()]
These are usually named 'layer-0/kernel'
and 'layer-0/bias'
. You can then access their values (after training your network) through your estimator (which I'll assume is named dnn_model
from your question). As an example:
weights_0 = dnn_model.get_variable_value(train_var_names[0])