Keras implementation levels: how do they work?

I'm starting to use Keras to create neural network models.

I have a classification problem where functions are discrete. To manage this case, the standard procedure is to convert discrete functions to binary arrays using hot coding.

However, this step does not seem to be necessary with Keras, since you can simply use the Embedding layer to create a fully functional representation of these discrete objects.

How are these embeddings performed ?

I understand that if a discrete function fcan take values k, then the embedding layer creates a matrix with columns k. Every time I get a value for this function, tell me i, at the training stage, only the imatrix column will be updated .

Do I understand correctly?

+5
source share
3 answers

, one-hot Embedding , . , Embedding . gather backend. , Embedding .

+4

Embedded Keras - .

.

.

, Keras Embedding .

:

e = Embedding(1000, 64, input_length=50)

Embedding 3 .

1000 , 1000 . 64 , 64 . 50 , 50 .

, .

Embedded.

, Embedding - , Embedding.

Embedded 2D- ( ).

. Embedded, 2D 1D , Flatten.

0

, N , . .

, - . ( ), .

object_index_1: vector_1
object_index_1: vector_2
...
object_index_n: vector_n

:

enter image description here

v - , , . - .

, :

  1. .
objects = ['cat', 'dog', 'snake', 'dog', 'mouse', 'cat', 'dog', 'snake', 'dog']
  1. ( ).
unique = ['cat', 'dog', 'snake', 'mouse'] # list(set(objects))
objects_index = [0, 1, 2, 1, 3, 0, 1, 2, 1] #map(unique.index, objects)

  1. (, )
objects_one_hot = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], 
     [0, 0 , 0, 1], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0]] # map(lambda x: [int(i==x) for i in range(len(unique))], objects_index)
#objects_one_hot is matrix is 4x9
  1. :
#M = matrix of dim x 4 (where dim is the number of dimensions you want the vectors to have). 
#In this case dim=2
M = np.array([[1, 1], [1, 2], [2, 2], [3,3]]).T # or... np.random.rand(2, 4)
#objects_vectors = M * objects_one_hot
objects_vectors = [[1, 1], [1, 2], [2, 2], [1, 2], 
    [3, 3], [1, 1], [1, 2], [2,2], [1, 2]] # M.dot(np.array(objects_one_hot).T)

, . !

, , . , , .

M , , , .

0
source

Source: https://habr.com/ru/post/1672157/


All Articles