I want to exchange Tensor a to a NumPy two-dimensional array for getting a heat map.
(Tensor(1, 64, 64, 1) -> numpy (64, 64))
I tried x.numpy(), but it doesn't work. The error message is this:
AttributeError: in user code: :187 call * x_np=x.numpy() /usr/local/lib/python3.7/dist packages/tensorflow/python/framework/ops.py:401 getattr self.getattribute(name) AttributeError: 'Tensor' object has no attribute 'numpy'
This is the tensor information and type of the tensor:
Tensor("ExpandDims:0", shape=(1, 64, 64, 1), dtype=float32)
<class 'tensorflow.python.framework.ops.Tensor'>
and I don't know what ExpandDims:0 means (what is this argv?).
Tensorflow:2.6.0
Numpy:1.21.2
I made the test code that has the same Tensor type, shape, and env, and it works.
What should I do?
import tensorflow as tf
import numpy
print(tf.__version__)
print(numpy.__version__)
a = tf.constant([[[[0.1]*1]*64]*64])
print(a)
print(a.numpy())
Related
I try to use 2D sparse input with Tensorflow 2.6, a minimal example is:
input1=keras.layers.Input(shape=(3,64), sparse=True)
layer1=keras.layers.Dense(32)(input1)
output1=keras.layers.Dense(32)(layer1)
model = keras.Model(inputs = [input1], outputs = [output1])
model.compile()
model.summary()
However I end up with the following error message:
TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("Placeholder_1:0", shape=(None, 3), dtype=int64), values=Tensor("Placeholder:0", shape=(None,), dtype=float32), dense_shape=Tensor("PlaceholderWithDefault:0", shape=(3,), dtype=int64)). Consider casting elements to a supported type.
What am I doing wrong ? it works if I flatten the matrix.
Edited code:
import tensorflow as tf
input1 = tf.keras.layers.Input(shape=(3,), sparse=True)
layer1 = tf.keras.layers.Dense(32)(input1)
output1= tf.keras.layers.Dense(32)(layer1)
model = tf.keras.Model(inputs = [input1], outputs = [output1])
model.compile()
model.summary()
Reference:
https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/guide/sparse_tensor.ipynb#scrollTo=E8za5DK8vfo7
I am building sample Neural Network using Pycharm, tensorflow 2.4 and python v3.8.5. When Running this command:
X = tf.placeholder("float", [None, num_input]) #num_input is the sized of input vector
I get an error like this one:
raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
TypeError: Error converting shape to a TensorShape: Dimension value must be integer or None or have an __index__ method, got value '8.0' with type '<class 'numpy.float64'>'.
What is the problem of that error? thanks in advance
This row (tf.placeholder) is enable for tensorflow 1, but you have installed tensorflow2. Disable Tf2 and run your backend on Tf1 .
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
X = tf.placeholder("float", [None, 8])
print(X)
<tf.Tensor 'Placeholder:0' shape=(?, 8) dtype=float32>
I'm trying to load images into a model using datasets. However, I keep getting an error that my tensor slices don't have the get_shape() attribute. I have confirmed that they don't by trying to call it within my code. What am I doing wrong?
I'm using:
Spyder 4.1.5 on Anaconda |
Keras 2.3.1 |
Tensorflow 2.1.0
# load dataset
dataset = h5py.File('3dshapes.h5', 'r')
print(dataset.keys())
images = dataset['images'] # array shape [480000,64,64,3], uint8 in range(256)
labels = dataset['labels'] # array shape [480000,6], float64
train_dataset = tf.data.Dataset.from_tensor_slices((images[1:10], labels[1:10]))
test_dataset = tf.data.Dataset.from_tensor_slices((images[10:20], labels[10:20]))
print("train_dataset", train_dataset)
print("test_dataset", test_dataset)
train_dataset <TensorSliceDataset shapes: ((64, 64, 3), (6,)), types:
(tf.uint8, tf.float64)>
test_dataset <TensorSliceDataset shapes: ((64, 64, 3), (6,)), types: (tf.uint8, tf.float64)>
File "C:\Users\Administration
User.conda\envs\Keras\lib\site-packages\tensorflow_core\python\keras\layers\convolutional.py",
line 192, in call
call_input_shape = inputs.get_shape()
AttributeError: 'TensorSliceDataset' object has no attribute
'get_shape'
Turns out I just needed to setup a new Python environment on Anaconda and install Tensorflow and Keras. My existing environment was already setup with these, but various other posts mentioned that doing this can help, and it did.
What is going on, and how do I correctly use Keras' Minimum() layer?
start = Input(shape=(28,28,1), dtype='float32')
c = Conv2D(20,3,use_bias=False,data_format="channels_last",input_shape=(28, 28, 1),padding='same')(start)
c = Reshape((28,28,20,1))(c)
m = MaxPooling3D(pool_size=(1,1,20), strides=1, padding='valid', data_format="channels_last")(c)
m = Reshape((28,28,1))(m)
m = Minimum()([m,mask])
print(m.shape, mask.shape)
f = Flatten()(m)
print(f.shape)
out = Dense(10,activation='softmax')(f)
model = Model(inputs=start, outputs=out)
output:
(60000, 28, 28, 1) (60000, 28, 28, 1)
(60000, 784)
AttributeError Traceback (most recent call last)
<ipython-input-21-e0fab99c12c2> in <module>()
9 print(f.shape)
10 out = Dense(10,activation='softmax')(f)
---> 11 model = Model(inputs=start, outputs=out)
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
The keras API says that Minimum() is:
Layer that computes the minimum (element-wise) a list of inputs.
It takes as input a list of tensors, all of the same shape,
and returns a single tensor (also of the same shape).
So it seems like I am using it correctly to me... but I am probably doing something very stupid.
I solved the problem. It's my mistake.
I was trying to pass mask to minimum(), but mask was a numpy array and minimum() needs a Keras Input()
I'm learning deep learning and trying to write some models. But I've stucked at data set. When I use ready code and data-set from github it works normally, but when I try my data set with same code it doesn't work. However , both datasets have same type and shape:
working dataset:
Shape of train: (5000, 32, 32, 3)
Type of train: <class 'numpy.ndarray'>
Shape of train labels: (5000,)
Shape of valid: (500, 32, 32, 3)
Shape of valid labels: (500,)
my data-set:
Shape of train: (31368, 32, 32, 3)
Type of train: <class 'numpy.ndarray'>
Shape of train labels: (31368,)
Shape of valid: (7841, 32, 32, 3)
Shape of valid labels: (7841, 32, 32, 3)
Shape of train_pixels[0]: (32, 32, 3)
Error I got:
ValueError: Error when checking model input: the list of Numpy arrays
that you are passing to your model is not the size the model expected.
Expected to see 1 arrays but instead got the following list of 7841
arrays: [array([[[186, 182, 255],
[179, 177, 255],
[163, 161, 244],...
There is one similar question I have found here but I couldn't use it, I got other errors. This solution doesn't work too.