How to get the dimensions of a tensor (in TensorFlow) at graph construction time?

I see most people confused about tf.shape(tensor) and tensor.get_shape() Let’s make it clear: tf.shape tf.shape is used for dynamic shape. If your tensor’s shape is changable, use it. An example: a input is an image with changable width and height, we want resize it to half of its size, then we can write something like: … Read more

How can I print the values of Keras tensors?

Keras’ backend has print_tensor which enables you to do this. You can use it this way: import keras.backend as K def loss_fn(y_true, y_pred): y_true = K.print_tensor(y_true, message=”y_true = “) y_pred = K.print_tensor(y_pred, message=”y_pred = “) … The function returns an identical tensor. When that tensor is evaluated, it will print its content, preceded by message. … Read more

Is .data still useful in pytorch?

.data was an attribute of Variable (object representing Tensor with history tracking e.g. for automatic update), not Tensor. Actually, .data was giving access to the Variable‘s underlying Tensor. However, since PyTorch version 0.4.0, Variable and Tensor have been merged (into an updated Tensor structure), so .data disappeared along the previous Variable object (well Variable is … Read more

What is a batch in TensorFlow?

Let’s say you want to do digit recognition (MNIST) and you have defined your architecture of the network (CNNs). Now, you can start feeding the images from the training data one by one to the network, get the prediction (till this step it’s called as doing inference), compute the loss, compute the gradient, and then … Read more

tf.shape() get wrong shape in tensorflow

tf.shape(input, name=None) returns a 1-D integer tensor representing the shape of input. You’re looking for: x.get_shape() that returns the TensorShape of the x variable. Update: I wrote an article to clarify the dynamic/static shapes in Tensorflow because of this answer: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/

PyTorch: How to get the shape of a Tensor as a list of int

For PyTorch v1.0 and possibly above: >>> import torch >>> var = torch.tensor([[1,0], [0,1]]) # Using .size function, returns a torch.Size object. >>> var.size() torch.Size([2, 2]) >>> type(var.size()) <class ‘torch.Size’> # Similarly, using .shape >>> var.shape torch.Size([2, 2]) >>> type(var.shape) <class ‘torch.Size’> You can cast any torch.Size object to a native Python list: >>> list(var.size()) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)