I am trying to write a function to create a CNN model. I get the following error whenever I run the script:
lua:15: unexpected symbol near '['
require('torch')
require('nn')
function CeateNvidiaModel()
--The Nvidia model
--Input dimensions
local n_channels = 3
local height = 66
local width = 200
local nvidia_model = nn.Sequential();
--nvida_model:add(nn.Normalize()
--Convolutional Layers
nvidia_model:add(nn.SpatialConvolution(n_channels, 24, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(24, 36, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(36, 48, 5, 5, [2], [2]))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(48, 64, 3, 3))
nvidia_model:add(nn.ELU(true))
nvidia_model:add(nn.SpatialConvolution(64, 64, 3, 3))
nvidia_model:add(nn.ELU(true))
-- Flatten Layer
nvidia_model:add(nn.Reshape(1164))
-- FC Layers
nvida_model:add(nn.Linear(1164, 100))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(100, 50))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(50, 10))
nvidia_model:add(nn.ELU(true))
nvida_model:add(nn.Linear(10, 1))
return nvida_model
end
I assume you are confusing [] and {}. In many other languages, you write array literals as [1, 2, 3], but in Lua [ and ] are only used for indexing; to declare an "array literal", you write {1, 2, 3} (because arrays in Lua are just tables).
The error message is a bit misleading; it says unexpected symbol near '[', but in reality the [ is the unexpected symbol.
Related
The desired operation is similar in spirit to torch.Tensor.index_copy, but a little different.
It's best explained with an example.
Tensor A has original values that we will copy:
[10, 20, 30]
Tensor B has indices of A:
[0, 1, 0, 1, 2, 1]
Tensor C has same length as B, containing the indexed values of A:
[10, 20, 10, 20, 30, 20]
What's a good way to make C from A and B in PyTorch, without using loops?
Have you tried just indexing by A?
In [1]: import torch
In [2]: a = torch.tensor([20,30,40])
In [3]: b = torch.tensor([0,1,2,1,1,2,0,0,1,2])
In [4]: a[b]
Out[4]: tensor([20, 30, 40, 30, 30, 40, 20, 20, 30, 40])
I am trying to create a matrix m x n x 3 base on some joints with cardinal cartesian x,y,z. First, I arrange the indices of the joints in the first image skeleton image into a 2D grid 2D grid
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
What I cannot do is to add the cartesian coordinate (x,y,z) of those indices along the third dimension of my matrice A in order to get m x n x 3. The x,y,z of each joint will be similar to the Chanel R, G, B of a color image with R=x, g=y, b=z
The the example code below produces a matrix mxnx3 from matrix A, where A illustrate indexes of elements in a skeleton, resulting in a A_result like:
# Create mxnx3 matrix from matrix A, where A include index name of skeleton element in each mxn location
import numpy as np
from matplotlib import pyplot as plt
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
#Let's rearrange slightly...
A_sub=A[0][:][:]
#Take the size of the matrix...
(m_max,n_max)=A_sub.shape
#Let's create basis for result matrix...note the format where mxn is first and after them the 3 dimensions of "colors"
A_result=np.zeros((A.shape[1],A.shape[2],3))
#demonstration function for the xyz coordinates...
def tell_me_xyz_coordinate_of_element(element_number):
#...perhaps in real application there is some measurement or the like functionality...
#...which investigate the element_number and then gives back its location...
#...but here to exemplify we simple return random int values back...
x=np.random.randint(0,255)
y=np.random.randint(0,255)
z=np.random.randint(0,255)
return x,y,z
#let's create the result matrix...
for m in range(m_max):
for n in range(n_max):
#Define x,y,z -values of the element in this m,n coordinate,
#where the value in m,n coordinate tells the number of corresponding element...
element_number=A_sub[m][n]
(x,y,z)=tell_me_xyz_coordinate_of_element(element_number)
#Set the results in the matrix...
A_result[m][n][0]=x
A_result[m][n][1]=y
A_result[m][n][2]=z
#Let's investigate the resulting nympy-matrix as a image...
#...remember to change the data format to uint8 to be able to investigate as a image
plt.imshow(np.uint8(A_result),interpolation='nearest')
title_text=''.join(["Result matrix mxnx3, \nwhere m=",str(m_max+1), " n=",str(n_max+1),",\n" "with color codes 0-255 in each m and n"])
plt.title(title_text)
plt.show()
This example may help you to solve your problem, but if not, please describe your question again in a more clear format.
#Create mxnx3 arrays and tensor form specific A matrix and then let's show how to create mxnx3 np-array and the same also in tensor format
import numpy as np
import tensorflow as tf
A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
#Let's convert A to a tensor:
A_in_tensorformat=tf.Variable(A)
#Let's make a numpy of size mxnx3:
m=123
n=45
B=np.ones((m,n,3))
B_in_tensorformat=tf.Variable(B)
I currently have a function that computes a sliding sum across a 1-D numpy array (vector) using convolve and hstack. I would like to create an equivalent function using dask, but the various ways I've tried so far have not worked out.
What I'm trying to do is to compute a "sliding sum" of n numbers of an array, unless any of the numbers are NaN in which case the sum should also be NaN. The (n - 1) elements of the result should also be NaN, since no wrap around/reach behind is assumed.
For example:
input vector: [3, 4, 6, 2, 1, 3, 5, np.NaN, 8, 5, 6]
n: 3
result: [NaN, NaN, 13, 12, 9, 6, 9, NaN, NaN, NaN, 19]
or
input vector: [1, 5, 7, 2, 3, 4, 9, 6, 3, 8]
n: 4
result: [NaN, NaN, NaN, 15, 17, 16, 18, 22, 22, 26]
The function I currently have for this using numpy functions:
def sum_to_scale(values, scale):
# don't bother if the number of values to sum is 1 (will result in duplicate array)
if scale == 1:
return values
# get the valid sliding summations with 1D convolution
sliding_sums = np.convolve(values, np.ones(scale), mode="valid")
# pad the first (n - 1) elements of the array with NaN values
return np.hstack(([np.NaN] * (scale - 1), sliding_sums))
How can I do the above using the dask array API (and/or dask_image.ndfilters) to achieve the same functionality?
>>> t = Tokenizer(num_words=3)
>>> l = ["Hello, World! This is so&#$ fantastic!", "There is no other world like this one"]
>>> t.fit_on_texts(l)
>>> t.word_index
{'fantastic': 6, 'like': 10, 'no': 8, 'this': 2, 'is': 3, 'there': 7, 'one': 11, 'other': 9, 'so': 5, 'world': 1, 'hello': 4}
I'd have expected t.word_index to have just the top 3 words. What am I doing wrong?
There is nothing wrong in what you are doing. word_index is computed the same way no matter how many most frequent words you will use later (as you may see here). So when you will call any transformative method - Tokenizer will use only three most common words and at the same time, it will keep the counter of all words - even when it's obvious that it will not use it later.
Just a add on Marcin's answer ("it will keep the counter of all words - even when it's obvious that it will not use it later.").
The reason it keeps counter on all words is that you can call fit_on_texts multiple times. Each time it will update the internal counters, and when transformations are called, it will use the top words based on the updated counters.
Hope it helps.
Limiting num_words to a small number (eg, 3) has no effect on fit_on_texts outputs such as word_index, word_counts, word_docs. It does have effect on texts_to_matrix. The resulting matrix will have num_words (3) columns.
>>> t = Tokenizer(num_words=3)
>>> l = ["Hello, World! This is so&#$ fantastic!", "There is no other world like this one"]
>>> t.fit_on_texts(l)
>>> print(t.word_index)
{'world': 1, 'this': 2, 'is': 3, 'hello': 4, 'so': 5, 'fantastic': 6, 'there': 7, 'no': 8, 'other': 9, 'like': 10, 'one': 11}
>>> t.texts_to_matrix(l, mode='count')
array([[0., 1., 1.],
[0., 1., 1.]])
Just to add a little bit to farid khafizov's answer,
words at sequence of num_words and above are removed from the results of texts_to_sequences (4 in 1st, 5 in 2nd and 6 in 3rd sentence disappeared respectively)
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
print(tf.__version__) # 2.4.1, in my case
sentences = [
'I love my dog',
'I, love my cat',
'You love my dog!'
]
tokenizer = Tokenizer(num_words=4)
tokenizer.fit_on_texts(sentences)
word_index = tokenizer.word_index
seq = tokenizer.texts_to_sequences(sentences)
print(word_index) # {'love': 1, 'my': 2, 'i': 3, 'dog': 4, 'cat': 5, 'you': 6}
print(seq) # [[3, 1, 2], [3, 1, 2], [1, 2]]
I am creating a convolution autoencoder in tensorflow. I got this exact error:
tensorflow.python.framework.errors.InvalidArgumentError: Conv2DBackpropInput: Number of rows of out_backprop doesn't match computed: actual = 8, computed = 12
[[Node: conv2d_transpose = Conv2DBackpropInput[T=DT_FLOAT, data_format="NHWC", padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/cpu:0"](conv2d_transpose/output_shape, Variable_1/read, MaxPool_1)]]
Relevant code:
l1d = tf.nn.relu(tf.nn.conv2d_transpose(l1da, w2, [10, 12, 12, 32], strides=[1, 1, 1, 1], padding='SAME'))
where
w2 = tf.Variable(tf.random_normal([5, 5, 32, 64], stddev=0.01))
I checked the shape of the input to conv2d_transpose i.e. l1da and it is correct(10x8x8x64). The batch size is 10, input to this layer is in the form of 8x8x64, and the output is supposed to be 12x12x32.
What am I missing?
Found the error. Padding should be "Valid", not "Same".