model <- lm(y~x, data)
print(model)
coef(model)
pred(model, newdata)
model = sklearn.linear_model.LinearRegression()
model.fit(x, y) # member function
model.predict(newdata)
model.coef_ # attribute
model.intercept_
library(glm)
import sklearn.linear_model
NOTE:
import numpy as np
import pandas as pd
import tensorly as tl
import
will import all the functions in the package.from ... import
just imports a part of the functions in the package.from tensorly.decomposition import parafac
from tensorly import random
import numpy as np
import pandas as pd
import tensorly as tl
numpy
: multi-dimensional container of generic datapandas
: data analysis and manipulation toolscipy
: scientific computing and technical computingsklearn
: machine learning librarymatplotlib
: plotting libraryseaborn
: data visualization library (fancier plotting library)keras
: neural-network librarya1 <- c(1,2,3) # vector
a2 <- matrix(1:24, 4, 6) # matrix
a3 <- array(1:24, dim = c(2,3,4)) # array
a4 <- data.frame(a1, a1) # data frame
Python:
Build-in structure
a1 = [1,2,3]
a2 = [[1,2,3], [4,5,6]] # list
a3 = (1,2,3) # tuple
Advanced structure imported from packages
import numpy as np
import pandas as pd
a1 = np.array([[1,2,3], [4,5,6]]) # matrix (array)
a1.shape
a1.ndim
a1.transpose()
a2 = pd.DataFrame([[1,2,3], [4,5,6]], columns=['a', 'b', 'c']) # data frame
a1 = np.array([[1,2,3], [4,5,6]])
a <- matrix(1:24, 4, 6)
a[1,1]
a = np.arange(24).reshape(4,6)
a[1,1]
a <- array(1:24, dim=c(2,3,4))
a[2,1,1]
a = np.arange(24).reshape(2,3,4)
a[1,0,0]
# Fibonacci number
a = [0,1]
i = 0
while True:
a.append(a[-1]+a[-2])
i += 1
if i >= 10:
break
Features
Install: Miniconda
conda create -n env_name python=3
-> source activate env_name
numpy
: conda install numpy
Python 2.x or Python 3.x: Download from Official website
Jupyter notebook:
conda install -c conda-forge notebook
pip install notebook
Note: Tensorly is written in Python3, which is totally different from Python2.
terminal
-> change to startup folder: cd /some_folder_name
-> jupyter notebook
run
-> cmd
-> jupyter notebook
How to change startup folder in Windowns? Refer to the website
M
Y
Two ways to install the packages
pip install -U tensorly
.conda install -c tensorly tensorly
.import tensorly as tl
import numpy as np
from tensorly import random
from tensorly.decomposition import parafac, tucker, matrix_product_state
from scipy.misc import face
import matplotlib.pyplot as plt
numpy
¶Create a random $2\times 3 \times 4$ array.
ary = np.array(np.arange(24).reshape(2,3,4))
For more details, please refer to Official API Reference
Create a random $2\times 3 \times 4$ tensor with tl.tensor
.
tensor = tl.tensor(ary)
tensor.shape
tensor
tensor[0,:,:]
Alternatively, another slicing method can be exclusively used for tensor object
tensor[0,...]
unfolded = tl.unfold(tensor, mode=0)
unfolded.shape
unfolded
The order in matricization is different from the typically used one in the literature or in R. Let $X \in \mathbb{R}^{p_1\times \ldots \times p_M}$, $X_{(n)}$ is the mode-$n$ matricization of $X$. Then the element $(i_1, \ldots, i_M)$ in $X$ is mapped to the element $(i_n, j)$, where $ j = \sum_{k=1, k\not=n}^M i_k \prod_{\ell > k} p_\ell. $
tensor[1,2,3]
tensor[0,1,3]
tl.fold(unfolded, mode=0, shape=tensor.shape)
tl.tensor_to_vec(tensor)
Let $X \in \mathbb{R}^{p_1\times \ldots \times p_M}$ and $v$ is the vecterization of $X$. For $v_j = X_{i_1, \ldots, i_M}$, we have $ j = \sum_{k=1}^M i_k \prod_{\ell>k} p_\ell $
tensor[1,2,3]
NOTE: Similarly, there are corresponding vectorization functions for CP, Tucker and TT form.
ttensor
, ktensor
random_tucker
, random_kruskal
, random_mps
(randomly generated)Note: Only Python provides Tensor-Train (TT) form.
cp_tensor = random.random_kruskal((10,8,6), rank=2, random_state=0)
tk_tensor = random.random_tucker((10,8,6), rank=2, random_state=0)
tt_tensor = random.random_mps((10,8,6), rank=(1,2,4,1), random_state=0)
tt_tensor[0]
tt_tensor[1]
tt_tensor[2]
full()
, tensor()
kruskal_to_tensor
, ...tl.kruskal_to_tensor(cp_tensor).shape
tl.tucker_to_tensor(tk_tensor[0], tk_tensor[1]).shape
tl.mps_to_tensor(tt_tensor).shape
kruskal_to_unfolded
, kruskal_to_vec
, ...tl.kruskal_to_unfolded(cp_tensor, mode=0).shape
tl.kruskal_to_vec(cp_tensor).shape
tensor_4D = tl.tensor(np.arange(48).reshape((2,3,4,2)))
tensor_4D
tl.partial_unfold(tensor_4D, skip_begin=1, mode=0)
tl.partial_tensor_to_vec(tensor_4D, skip_begin=1)
The same as the matricization at the first mode
tl.unfold(tensor_4D, mode=0)
ttm
, ttl
ttv
, ttm
mode_dot
, multi_mode_dot
tensor = tl.tensor(np.random.random(1000).reshape((10,10,10)))
v1 = np.random.random(10)
v2 = np.random.random(10)
tl.tenalg.multi_mode_dot(tensor, matrix_or_vec_list=[v1, v2], modes=[1,2])
U1 = np.random.random(20).reshape(2,10)
U2 = np.random.random(30).reshape(3,10)
U3 = np.random.random(40).reshape(4,10)
tl.tenalg.multi_mode_dot(tensor, matrix_or_vec_list=[U1, U2, U3])
Note: Khatri-Rao product, Kronecker product, inner products are also provided.
# from tensorly.decomposition import parafac, tucker, matrix_product_state
cp()
parafac_als()
parafac()
cp_tensor_full = tl.kruskal_to_tensor(cp_tensor) # Convert the cp decomposition to the full tensor.
Two ways of generating the intial factor matrices: random
and svd
.
parafac(tensor=cp_tensor_full, rank=2, init='random', n_iter_max=int(1e4), random_state=1)
parafac(tensor=cp_tensor_full, rank=2, init='svd', n_iter_max=int(1e4), random_state=1)
tucker()
tucker_als()
tucker()
NOTE: HOSVD algorithm is not included in Python.
tk_tensor_full = tl.tucker_to_tensor(tk_tensor[0], tk_tensor[1])
tucker(tk_tensor_full, ranks=(2,2,2), init='random', verbose=1, random_state=0)
matrix_product_state()
tt_tensor_full = tl.mps_to_tensor(tt_tensor)
tt_tensor_full.shape
matrix_product_state(tt_tensor_full, rank=(1,2,4,1), verbose=1)
Skipped
# from scipy.misc import face
# import matplotlib.pyplot as plt
image = tl.tensor(face(), dtype='float64')
image.shape
def to_image(tensor):
"""A convenience function to convert from a float dtype back to uint8"""
im = tl.to_numpy(tensor)
im -= im.min()
im /= im.max()
im *= 255
return im.astype(np.uint8)
random_state = 12345
# Rank of the CP decomposition
cp_rank = 25
# Perform the CP decomposition
factors = parafac(image, rank=cp_rank, init='random', tol=10e-6, random_state=random_state)
# Reconstruct the image from the factors
cp_reconstruction = tl.kruskal_to_tensor(factors)
# Rank of the Tucker decomposition
tucker_rank = [100, 100, 2]
# Tucker decomposition
core, tucker_factors = tucker(image, ranks=tucker_rank, init='random', tol=10e-5, random_state=random_state)
# Reconstruct the image from the factors
tucker_reconstruction = tl.tucker_to_tensor(core, tucker_factors)
# Tensor-Train decomposition
tt_rank = [1, 100, 2, 1]
tt_factors = matrix_product_state(image, rank=tt_rank)
tt_reconstruction = tl.mps_to_tensor(tt_factors)
%timeit factors = parafac(image, rank=cp_rank, init='random', tol=10e-6, random_state = random_state)
%timeit core, tucker_factors = tucker(image, ranks=tucker_rank, init='random', tol=10e-5, random_state=random_state)
%timeit tt_factors = matrix_product_state(image, rank=tt_rank)
fig = plt.figure(figsize=(40,30))
ax1 = plt.subplot(221)
ax1.set_axis_off()
ax1.imshow(to_image(image))
ax1.set_title("Original")
ax1.title.set_size(50)
ax2 = plt.subplot(222)
ax2.set_axis_off()
ax2.imshow(to_image(cp_reconstruction))
ax2.set_title("CP decomposition")
ax2.title.set_size(50)
ax3 = plt.subplot(223)
ax3.set_axis_off()
ax3.imshow(to_image(tucker_reconstruction))
ax3.set_title("Tucker decomposition")
ax3.title.set_size(50)
ax4 = plt.subplot(224)
ax4.set_axis_off()
ax4.imshow(to_image(tt_reconstruction))
ax4.set_title("Tensor-Train decomposition")
ax4.title.set_size(50)