paulgavrikov / visualkeras

Visualkeras is a Python package to help visualize Keras (either standalone or included in TensorFlow) neural network architectures. It allows easy styling to fit most needs. This module supports layered style architecture generation which is great for CNNs (Convolutional Neural Networks), and a graph style architecture, which works great for most models including plain feed-forward networks.
MIT License
582 stars 67 forks source link

tfp layers not working #35

Closed markub3327 closed 1 year ago

markub3327 commented 1 year ago

Hi,

I try to visualise the tfpl.IndependentNormal, but it returns an error:


TypeError Traceback (most recent call last) in <cell line: 1>() ----> 1 visualkeras.layered_view(vae, legend=True)

/usr/local/lib/python3.9/dist-packages/visualkeras/layered.py in layered_view(model, to_file, min_z, min_xy, max_z, max_xy, scale_z, scale_xy, type_ignore, index_ignore, color_map, one_dim_orientation, background_fill, draw_volume, padding, spacing, draw_funnel, shade_step, legend, font, font_color) 105 y = min(max(shape[1] scale_xy, y), max_xy) 106 elif one_dim_orientation == 'z': --> 107 z = min(max(shape[1] scale_z, z), max_z) 108 else: 109 raise ValueError(f"unsupported orientation {one_dim_orientation}")

TypeError: can't multiply sequence by non-int of type 'float'

paulgavrikov commented 1 year ago

Hi,this is a problem with the pypi hosted version, please install visualkeras directly from GitHub (see #8)

markub3327 commented 1 year ago

@paulgavrikov

I tried it again with: !pip3 install git+https://github.com/paulgavrikov/visualkeras

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting git+https://github.com/paulgavrikov/visualkeras Cloning https://github.com/paulgavrikov/visualkeras to /tmp/pip-req-build-yyfnfdjb Running command git clone --filter=blob:none --quiet https://github.com/paulgavrikov/visualkeras /tmp/pip-req-build-yyfnfdjb Resolved https://github.com/paulgavrikov/visualkeras to commit cd169b81be347e2090353ad6fe2bd2e1f4020cf4 Preparing metadata (setup.py) ... done Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.9/dist-packages (from visualkeras==0.0.2) (8.4.0) Requirement already satisfied: numpy>=1.18.1 in /usr/local/lib/python3.9/dist-packages (from visualkeras==0.0.2) (1.22.4) Collecting aggdraw>=1.3.11 Downloading aggdraw-1.3.16-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (992 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 992.0/992.0 kB 18.7 MB/s eta 0:00:00 Building wheels for collected packages: visualkeras Building wheel for visualkeras (setup.py) ... done Created wheel for visualkeras: filename=visualkeras-0.0.2-py3-none-any.whl size=13739 sha256=e25bc44d4912c5a4d74219bdf5be901127b9ab7493fd2183d8938aeaff89daa9 Stored in directory: /tmp/pip-ephem-wheel-cache-hq6etyjb/wheels/3a/96/3c/e6bcf58185d2017dc7b072e5794e6b1dd034d1bdb7d15e2463 Successfully built visualkeras Installing collected packages: aggdraw, visualkeras Successfully installed aggdraw-1.3.16 visualkeras-0.0.2

The issue:


TypeError Traceback (most recent call last) in <cell line: 1>() ----> 1 visualkeras.layered_view(vae, legend=True)

/usr/local/lib/python3.9/dist-packages/visualkeras/layered.py in layered_view(model, to_file, min_z, min_xy, max_z, max_xy, scale_z, scale_xy, type_ignore, index_ignore, color_map, one_dim_orientation, background_fill, draw_volume, padding, spacing, draw_funnel, shade_step, legend, font, font_color) 103 x = min(max(shape[0] scale_xy, x), max_xy) 104 y = min(max(shape[1] scale_xy, y), max_xy) --> 105 z = min(max(self_multiply(shape[2:]) * scale_z, z), max_z) 106 107 box = Box()

TypeError: can't multiply sequence by non-int of type 'float'

paulgavrikov commented 1 year ago

Can you call layer.output_shape on the layer that causes the issue and tell me the output?

markub3327 commented 1 year ago

Here is complete code for reconstruction of issue:

tfd = tfp.distributions
tfpl = tfp.layers
tfk = tf.keras
tfkl = tf.keras.layers

# Create a stochastic encoder -- e.g., for use in a variational auto-encoder.
input_shape = [28, 28, 1]
encoded_shape = 2
encoder = tfk.Sequential([
  tfkl.InputLayer(input_shape=input_shape),
  tfkl.Flatten(),
  tfkl.Dense(10, activation='relu'),
  tfkl.Dense(tfpl.IndependentNormal.params_size(encoded_shape)),
  tfpl.IndependentNormal(encoded_shape)
])
print(encoder.layers[-1].output_shape)
print(encoder.output_shape)

visualkeras.layered_view(encoder, legend=True)

Result

((None, 2), (None, 2)) (None, 2)

TypeError Traceback (most recent call last) in <cell line: 13>() 11 print(encoder.layers[-1].output_shape) 12 print(encoder.output_shape) ---> 13 visualkeras.layered_view(encoder, legend=True)

/usr/local/lib/python3.9/dist-packages/visualkeras/layered.py in layered_view(model, to_file, min_z, min_xy, max_z, max_xy, scale_z, scale_xy, type_ignore, index_ignore, color_map, one_dim_orientation, background_fill, draw_volume, padding, spacing, draw_funnel, shade_step, legend, font, font_color) 103 x = min(max(shape[0] scale_xy, x), max_xy) 104 y = min(max(shape[1] scale_xy, y), max_xy) --> 105 z = min(max(self_multiply(shape[2:]) * scale_z, z), max_z) 106 107 box = Box()

TypeError: can't multiply sequence by non-int of type 'float'

github-actions[bot] commented 1 year ago

Stale issue message