logo
down
shadow

theano ~ use an index matrix and embeddings matrix to produce a 3D tensor?


theano ~ use an index matrix and embeddings matrix to produce a 3D tensor?

Content Index :

theano ~ use an index matrix and embeddings matrix to produce a 3D tensor?
Tag : python , By : markku
Date : November 24 2020, 09:00 AM

it should still fix some issue Let's say I have an index matrix: , You can simply index into the embedding matrix with the index matrix:
import numpy
import theano
import theano.tensor as tt

embeddings_matrix = theano.shared(numpy.array([[1, 2, 3], [2, 3, 4], [4, 5, 6]], dtype=theano.config.floatX))
index_matrix = tt.imatrix()
y = embeddings_matrix[index_matrix]
f = theano.function([index_matrix], y)
output = f(numpy.array([[0, 1], [2, 1]], dtype=numpy.int32))
print output.shape, '\n', output
(2L, 2L, 3L) 
[[[ 1.  2.  3.]
  [ 2.  3.  4.]]

 [[ 4.  5.  6.]
  [ 2.  3.  4.]]]

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Indexing tensor with index matrix in theano?


Tag : python , By : gcomstock
Date : March 29 2020, 07:55 AM
This might help you I have a theano tensor A such that A.shape = (40, 20, 5) and a theano matrix B such that B.shape = (40, 20). Is there a one-line operation I can perform to get a matrix C, where C.shape = (40, 20) and C(i,j) = A[i, j, B[i,j]] with theano syntax? , You can do the following in numpy:
import numpy as np

A = np.arange(4 * 2 * 5).reshape(4, 2, 5)
B = np.arange(4 * 2).reshape(4, 2) % 5

C = A[np.arange(A.shape[0])[:, np.newaxis], np.arange(A.shape[1]), B]
import theano
import theano.tensor as T

AA = T.tensor3()
BB = T.imatrix()

CC = AA[T.arange(AA.shape[0]).reshape((-1, 1)), T.arange(AA.shape[1]), BB]

f = theano.function([AA, BB], CC)

f(A.astype(theano.config.floatX), B)

Theano matrix multiplication of 2d matrix to give 3d matrix


Tag : python , By : Shane
Date : March 29 2020, 07:55 AM
To fix this issue I want to do exactly what the following question is trying to do in numpy: , As per @Divakar s comment, change it to
x[:,:,None] * y[:,None,:]

How to use integer values of a matrix as index for another matrix using numpy or Theano?


Tag : python , By : user184975
Date : March 29 2020, 07:55 AM
may help you . Here's a vectorized approach using broadcasting -
# Get mask of matching elements against the iterators
m,n = I.shape
Imask = I == np.arange(m)[:,None,None,None]
Jmask = J == np.arange(n)[:,None,None]

# Get the mask of intersecting ones
mask = Imask & Jmask

# Get D intersection masked array
Dvals = np.where(mask,D,np.inf)

# Get argmin along merged last two axes. Index into flattened V for final o/p
out = V.ravel()[Dvals.reshape(m,n,-1).argmin(-1)]
In [136]: I = np.array([[0,1,2],[1,1,0],[0,0,2]])
     ...: J = np.array([[1,1,1],[1,2,1],[0,1,0]])
     ...: D = np.array([[1.2, 3.4, 2.2],[2.2, 4.3, 2.3],[7.1, 6.1, 2.7]])
     ...: V = np.array([[1.1 , 8.1, 9.1],[3.1, 7.1, 2.1],[0.1, 5.1, 3.1]])
     ...: 

In [144]: out
Out[144]: 
array([[ 0.1,  1.1,  1.1], # To verify : v[0,1] = 1.1
       [ 1.1,  3.1,  7.1],
       [ 3.1,  9.1,  1.1]])

How to understand a theano tensor as a matrix and a keras.backend variable?


Tag : python , By : Keonne Rodriguez
Date : March 29 2020, 07:55 AM
like below fixes the issue According to the source code, keras creates this in theano:
variable = theano.shared(value=np.zeros(shape),
                         name='somename',
                         strict=False)
variable._keras_shape = value.shape
variable._uses_learning_phase = False

Why the first index of a multidimensional matrix of Eigen::Tensor able to loop through all the members of the tensor?


Tag : development , By : Tony Z
Date : March 29 2020, 07:55 AM
should help you out As of today, the behavior of accessing out-of-range elements of an Eigen::Tensor is not documented. But you can actually have it generate assertions when compiling with -DEIGEN_INTERNAL_DEBUGGING (however, this can make execution significantly slower).
Godbolt-Demo: https://godbolt.org/z/RrnjXS
Related Posts Related QUESTIONS :
  • some coordinates that I extracted from geocoder in Python are not saving in the variable I created
  • 7C in cs circles- python Im not sure what is wrong with this yet
  • How to fix 'AttributeError: 'list' object has no attribute 'shape'' error in python with Tensorflow / Keras when loading
  • python - thread`s target is a method of an object
  • Retrieve Variable From Class
  • What is the reason for matplotlib for printing labels multiple times?
  • Why would people use ThreadPoolExecutor instead of direct function call?
  • When clear_widgets is called, it doesnt remove screens in ScreenManager
  • Python can't import function
  • Pieces doesn't stack after one loop on my connect4
  • How to change font size of all .docx document with python-docx
  • How to store a word with # in .cfg file
  • How to append dictionaries to a dictionary?
  • How can I scrape text within paragraph tag with some other tags then within the paragraph text?
  • Custom entity ruler with SpaCy did not return a match
  • Logging with two handlers - one to file and one to stderr
  • How to do pivot_table in dask with aggfunc 'min'?
  • This for loop displays only the last entry of the student record
  • How to split a string by a specific pattern in number of characters?
  • Python 3: how to scrape research results from a website using CSFR?
  • Setting the scoring parameter of RandomizedSeachCV to r2
  • How to send alert or message from view.py to template?
  • How to add qml ScatterSeries to existing qml defined ChartView?
  • Django + tox: Apps aren't loaded yet
  • My css and images arent showing in django
  • Probability mass function sum 2 dice roll?
  • Cannot call ubuntu 'ulimit' from python subprocess without using shell option
  • Dataframe Timestamp Filter for new/repeating value
  • Problem with clicking select2 dropdownlist in selenium
  • pandas dataframe masks to write values into new column
  • How to click on item in navigation bar on top of page using selenium python?
  • Add multiple EntityRuler with spaCy (ValueError: 'entity_ruler' already exists in pipeline)
  • error when replacing missing ')' using negative look ahead regex in python
  • Is there a way to remove specific strings from indexes using a for loop?
  • select multiple tags by position in beautifulSoup
  • pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdout
  • Shipping PyGObject/GTK+ app on Windows with MingW
  • Python script to deduplicate lines in multiple files
  • How to prevent window and widgets in a pyqt5 application from changing size when the visibility of one widget is altered
  • How to draw stacked bar plot from df.groupby('feature')['label'].value_counts()
  • Python subprocess doesn't work without sleep
  • How can I adjust 'the time' in python with module Re
  • Join original np array with resulting np array in a form of dictionary? multidimensional array? etc?
  • Forcing labels on histograms in each individual graph in a figure
  • For an infinite dataset, is the data used in each epoch the same?
  • Is there a more efficent way to extend a string?
  • How to calculate each single element of a numpy array based on conditions
  • How do I change the width of Jupyter notebook's cell's left part?
  • Measure distance between lat/lon coordinates and utm coordinates
  • Installing megam for NLTK on Windows
  • filter dataframe on each value of a samn column have a specific value of another column in Panda\Python
  • Threading with pubsub throwing AssertionError: 'callableObj is not callable' in wxPython
  • Get grouped data from 2 dataframes with condition
  • How can I import all of sklearns regressors
  • How to take all elements except the first k
  • Whats wrong with my iteration list of lists from csv
  • Tensorflow Estimator API save image summary in eval mode
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com