Skip to content
Advertisement

Keras flatten: ValueError: Attempt to convert a value (None) with an unsupported type () to a Tensor

I have the error mentioned in the title, with the following code

import tensorflow as tf
import numpy as np
import pandas as pd
import seaborn as sns
from random import shuffle
import datetime
from matplotlib import pyplot

from numpy import mean
from numpy import std
from matplotlib import pyplot
from sklearn.model_selection import KFold
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.optimizers import SGD

from tensorflow.keras.models import load_model

first_branch= Input(shape=(28,28,1))
first_branch_st1 = Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28,1))(first_branch)
first_branch_st2 = MaxPooling2D((2, 2))(first_branch_st1)
first_branch_st3 = Flatten()(first_branch_st2)
first_branch_st4 = Dense(100, activation='relu')(first_branch_st3)

This sends the following error

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_1973/3453405928.py in <module>
      2 first_branch_st1 = Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28,1))(first_branch)
      3 first_branch_st2 = MaxPooling2D((2, 2))(first_branch_st1)
----> 4 first_branch_st3 = Flatten()(first_branch_st2)
      5 first_branch_st4 = Dense(100, activation='relu')(first_branch_st3)
      6 

ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.

According to the question asked with the same error it happens when you mix up keras and tf.keras. But i think have defined the imports accordingly, so unless there is a clash between imports or a bad definition of them i do not think that’s the problem. There is another known solution?

Advertisement

Answer

There’s only 1 bug, Input is not defined but still unrelated to the error mentioned. Use below:

first_branch= tf.keras.Input(shape=(28,28,1))

My advice is to please check on the latest versions of tf e.g. 2.4.1.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement