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 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.
I need to count the number of items in an array. Is there a function to do it? I can do it with a for loop but if there was a function it would be 100 times easier.
arr1 = [10, 12, 87, 36, 11, 9, 73]
for each in arr1:
x += 1
print x
You can try using the length function:
len(arr1)
First of all I´m new to Machine Learning.
I am trying to predict the price of second hand cars. This cars have makes and models, so I used a MultiLabelBinarizer to make a sparse matrix, to handle the categorical attributes, here's the code:
from sklearn.preprocessing import MultiLabelBinarizer
encoder = MultiLabelBinarizer()
make_cat_1hot = encoder.fit_transform(make_cat)
model_cat_1hot = encoder.fit_transform(model_cat)
type_cat_1hot = encoder.fit_transform(type_cat)
print(type(make_cat_1hot))
carInfoModHot = carsInfoMod.copy()
carInfoModHot["makeHot"] = make_cat_1hot.tolist()
carInfoModHot["modelHot"] = model_cat_1hot.tolist()
carInfoModHot["typeHot"] = type_cat_1hot.tolist()
doors km make year makeHot modelHot
5.0 78779 Mercedes 2012 [0, 0, 0, 0, 1, 0, 0, 0, ...[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, ...
5.0 25463 Bmw 2015 [0, 1, 0, 0, 0, 0, 0, ... [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, ...
Then I used it to make a prediction and get the mean square error with a Linear Regression:
lr = linear_model.LinearRegression()
carsInfoTrainHot = carInfoModHot.drop(["price"], axis=1) # drop labels for training set
df1 = carsInfoTrainHot.iloc[:30000, :]
carsLabels1 = carsInfoMod.iloc[:30000, 3]
print(carsInfoTrainHot.head())
df2 = carsInfoTrainHot.iloc[30001:60000, :]
carsLabels2 = carsInfoMod.iloc[30001:60000, 3]
df3 = carsInfoTrainHot.iloc[60001:, :]
carsLabels3 = carsInfoMod.iloc[60001:, 3]
lr.fit(df1, carsLabels1)
print(carsInfoTrainHot.shape)
carPrediction = lr.predict(df2)
lin_mse = mean_squared_error(carsLabels2, carPrediction)
lin_rmse = np.sqrt(lin_mse)
But I get this error:
ValueError Traceback (most recent call
last) in ()
12 carsLabels3 = carsInfoMod.iloc[60001:, 3]
13
---> 14 lr.fit(df1, carsLabels1)
15 print(carsInfoTrainHot.shape)
16 carPrediction = lr.predict(df2)
/home/vagrant/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/base.py
in fit(self, X, y, sample_weight)
510 n_jobs_ = self.n_jobs
511 X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 512 y_numeric=True, multi_output=True)
513
514 if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:
/home/vagrant/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py
in check_X_y(X, y, accept_sparse, dtype, order, copy,
force_all_finite, ensure_2d, allow_nd, multi_output,
ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype,
estimator)
519 X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
520 ensure_2d, allow_nd, ensure_min_samples,
--> 521 ensure_min_features, warn_on_dtype, estimator)
522 if multi_output:
523 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
/home/vagrant/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py
in check_array(array, accept_sparse, dtype, order, copy,
force_all_finite, ensure_2d, allow_nd, ensure_min_samples,
ensure_min_features, warn_on_dtype, estimator)
400 # make sure we actually converted to numeric:
401 if dtype_numeric and array.dtype.kind == "O":
--> 402 array = array.astype(np.float64)
403 if not allow_nd and array.ndim >= 3:
404 raise ValueError("Found array with dim %d. %s expected <= 2."
ValueError: setting an array element with a sequence.
From what I understand is that I´m inserting an array in the categorical attributes, but how else can I change the categorical values to a sparse matrix?
Thanks.
When I use Maxima to calculate the Taylor series:
f(x,y) := taylor((x+y)^3, [x, y], [2, 3], 2);
f(2,3); /* error: wrong number of arguments */
Basically I want to define a function as a expansion of (x+y)^3, which takes in x,y as parameter. How can I achieve this?
Try
(%i1) f(x,y) := ''(ratdisrep(taylor(('x+'y)^3, ['x, 'y], [2, 3], 2))) $
(%i2) f(2, 3);
(%o2) 125
or
(%i1) define(f(x, y), ratdisrep(taylor(('x+'y)^3, ['x, 'y], [2, 3], 2)))$
(%i2) f(2, 3);
(%o2) 125