tf tensor与numpy转换,embedding_lookup、embedding_lookup_sparse使用,feature_column使用

1、 tensor与numpy转换

注意:tf 2以后不用session ,tensor转numpy直接 mm.numpy()就行
np.asarray([1,3,6,2]) #列表或元祖转array

# numpy to tensor
import numpy as np
data_numpy = np.ones(5)
mm = tf.convert_to_tensor(data_numpy)
with tf.Session() as sess:
    print(mm.eval())
# tensor to numpy
with tf.Session() as sess:
    print(kk.eval())

2、embedding_lookup、embedding_lookup_sparse使用

参考:https://www.jianshu.com/p/e8986d0ff4ff

import tensorflow as tf
# tf.enable_eager_execution() # 关键
# tf.enable_eager_execution(
#     config=None,
#     device_policy=None,
#     execution_mode=None
# )


embedding = tf.constant(
    [
        [0.21,0.41,0.51,0.11],
        [0.22,0.42,0.52,0.12],
        [0.23,0.43,0.53,0.13],
        [0.24,0.44,0.54,0.14]
    ],dtype=tf.float32)

feature_batch = tf.constant([2,3,1,0])

get_embedding1 = tf.nn.embedding_lookup(embedding,feature_batch)

feature_batch_one_hot = tf.one_hot(feature_batch,depth=4)

get_embedding2 = tf.matmul(feature_batch_one_hot,embedding)
# print(get_embedding1.numpy().tolist())
with tf.Session() as sess:
    print(get_embedding1.eval())
    print(get_embedding2.eval())

tf tensor与numpy转换,embedding_lookup、embedding_lookup_sparse使用,feature_column使用_第1张图片

多值离散,multi

embedding = tf.constant(
    [
        [0.21,0.41,0.51,0.11],
        [0.22,0.42,0.52,0.12],
        [0.23,0.43,0.53,0.13],
        [0.24,0.44,0.54,0.14]
    ],dtype=tf.float32)

# sparse embedding
a = tf.SparseTensor(indices=[[0, 0],[1, 2],[1,3]], values=[1, 2, 3], dense_shape=[2, 4])

embedding_sparse = tf.nn.embedding_lookup_sparse(embedding, sp_ids=a, sp_weights=None)

with tf.Session() as sess:
#     sess.run(tf.global_variables_initializer())
    print(sess.run(embedding_sparse))
    print(embedding_sparse.eval())

tf tensor与numpy转换,embedding_lookup、embedding_lookup_sparse使用,feature_column使用_第2张图片

3、feature_column使用

参考:https://zhuanlan.zhihu.com/p/41663141

注意:crossed_column 类别特征交叉处理;
indicator_column 少量类别特征转化为连续形的onehot或multihot形式作为dnn输入;
embedding_column 大量类别特征转化为连续形的分布式表示形式作为dnn输入;
另外crossed_column,indicator_column,embedding_column这三个都是基于categorical_column进行进一步二次的处理,必须要先有categorical_column进行处理先

tf tensor与numpy转换,embedding_lookup、embedding_lookup_sparse使用,feature_column使用_第3张图片
tf tensor与numpy转换,embedding_lookup、embedding_lookup_sparse使用,feature_column使用_第4张图片

你可能感兴趣的:(深度学习)