Tutorial on classifying handwritten digits has wrong/missing attribute

Viewed 388

I am following the tutorial for Classifying Handwritten Digits with TF.Learn

The last part of the tutorial is where I have a problem. It seems like classifier.weights_ doesn't exist anymore.

This is the error message: AttributeError: 'LinearClassifier' object has no attribute 'weights_'

weights = classifier.weights_
f, axes = plt.subplots(2, 5, figsize=(10,4))
axes = axes.reshape(-1)
for i in range(len(axes)):
    a = axes[i]
    a.imshow(weights.T[i].reshape(28, 28), cmap=plt.cm.seismic)
    a.set_title(i)
    a.set_xticks(()) # ticks be gone
    a.set_yticks(())
plt.show()
1 Answers

TensorFlow has been changing pretty fast in the past couple of years, and anything using a version prior to 1.0 is likely to be out of date. The LinearClassifier.weights_ attribute has been removed and there doesn't seem to be an exact match for it. What you can do is ask for the list of variables and then pass the variable you want to weights. To do this, use

for var in classifier.get_variable_names():
    print("var:", var, "=", classifier.get_variable_value(var))

In my case this gives me three weight variables named "linear//weight", "linear//weight/d/linear//weight/part_0/Ftrl", and "linear//weight/d/linear//weight/part_0/Ftrl_1", along with some other stuff. These tensors are quite big, so their values are shown only in abbreviated form. Then you can pass one of them to weights:

weights = classifier.get_variable_value("linear//weight/d/linear//weight/part_0/Ftrl_1")

and you should see something like the image in the tutorial. All three of them are similar but they're not the same, so possibly LinearClassifier is doing something more complex than it used to.

Related