1

im trying to run the following code

import matplotlib as plt

def plot_filters(layer, x, y):

    filters = layer.get_weights()
    fig = plt.figure.Figure()

    for j in range(len(filters)):
        ax = fig.add_subplot(y, x, j+1)
        ax.matshow(filters[j][0], cmap = plt.cm.binary)
        plt.xticks(np.array([]))
        plt.yticks(np.array([]))

    plt.tight_layout()
    return plt

plot_filters(model.layers[0], 8, 4)

when running this I am receiving 'module' object is not callable and it is referencing the plt.tight_layout() line. Cant figure out how to call this. It is present in the matplotlib package.

Any help will be appreciated!

Thanks

8
  • Try with fig.tight_layout() Commented Oct 31, 2018 at 9:36
  • @nsaura unfortunately, when i run fig=plt.figure() it gives me the same error but instead it references the fig=plt.figure() line, Commented Oct 31, 2018 at 9:37
  • yes i misread the question i edited my first comment Commented Oct 31, 2018 at 9:40
  • @nsaura that seems to work (i can mark your answer if you post it); umm I have a new error now ValueError: too many values to unpack (expected 2). Can you possibly help me with that? Commented Oct 31, 2018 at 9:43
  • It's may be because of the return. Try to call p = plot_filters(model.layers[0], 8, 4) Commented Oct 31, 2018 at 9:45

2 Answers 2

5

You have imported the matplotlib module itself as plt, where you should be importing the pyplot module as plt instead:

import matplotlib.pyplot as plt
import matplotlib.cm as cm

def plot_filters(layer, x, y):

    filters = layer.get_weights()
    fig = plt.figure()

    for j in range(len(filters)):
        ax = fig.add_subplot(y, x, j+1)
        ax.matshow(filters[j][0], cmap = cm.binary)
        plt.xticks(np.array([]))
        plt.yticks(np.array([]))

    plt.tight_layout()

plot_filters(model.layers[0], 8, 4)
Sign up to request clarification or add additional context in comments.

7 Comments

i get an additional error ValueError: too many values to unpack (expected 2) is there any clue you can give to solve this? thanks! :)
Where do you get that error? Can you give the full traceback?
there you go Traceback (most recent call last): File "C:\Users\black\Desktop\Uni Work\2018\Sem 2\NIC\Assignment 2\Code\CNN.py", line 84, in <module> plot_filters(model.layers[0], 8, 4) File "C:\Users\black\Desktop\Uni Work\2018\Sem 2\NIC\Assignment 2\Code\CNN.py", line 20, in plot_filters ax.matshow(filters[j][0], cmap = plt.cm.binary) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\matplotlib\axes\_axes.py", line 7841, in matshow nr, nc = Z.shape ValueError: too many values to unpack (expected 2)
It's something to do with your data. I don't know what filters looks like so can't really say. It should be a 2D array
yea when I output the weights they show up as a 2D array but I can seem to load them
|
0

try to write it like this

import matplotlib.pyplot as plt
import matplotlib.cm as cm

def plot_filters(layer, x, y):

    filters = layer.get_weights()
    fig = plt.figure()

    for j in range(len(filters)):
        ax = fig.add_subplot(y, x, j+1)
        ax.matshow(filters[j][0], cmap = cm.binary)
        plt.xticks(np.array([]))
        plt.yticks(np.array([]))

    fig.tight_layout()

plot_filters(model.layers[0], 8, 4)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.