作为神经网络输入的特征

时间:2017-12-28 09:51:41

标签: python tensorflow

这就是我定义神经网络的方法

import tensorflow as tf

class MyFun:
    def __init__(self, x, y, sizes, activations, scope):
        with tf.variable_scope(scope):
            last_out = tf.concat([x, y], axis=1)
            for l, size in enumerate(sizes):
                last_out = tf.layers.dense(last_out, size, activation=activations[l])
            self.vars = tf.trainable_variables(scope=scope)
            self.output = last_out

我需要先将预处理输入xy(两个占位符)用于特征,然后再将它们输入网络。更具体地说,我想使用二次特征,即

new_input = [1, x, y, x**2, y**2, cross(x,y)]

其中cross(x,y)包含[x, y]所有元素之间的产品,即

cross(x,y) = [x_1*x_2, x_1*x_3, ..., x_1*y_1, ...]

我怎样才能优雅地做到这一点?是否有等价的sklearn.preprocessing.PolynomialFeatures

1 个答案:

答案 0 :(得分:1)

这是一个选项:

# Suppose your placeholders are one dimensional vectors, with sizes 3 and 7:
x = tf.placeholder(tf.float32,shape=[3])
y = tf.placeholder(tf.float32, shape=[7])

# concat the constant 1.0 with x and y:
z = tf.concat((tf.constant(1.0,shape=(1,)),x,y),axis=0)

# construct all products of pairs:    
new_input = [z[i]*z[j] for i in range(3+7-1) for j in range(i,3+7)]

# convert the list of tensors to a tensor (optional):
new_input = tf.stack(new_input)

编辑1

将此扩展到xy具有批量维度的情况:

x = tf.placeholder(tf.float32,shape=[None,3])
y = tf.placeholder(tf.float32, shape=[None,7])

# I use 1.0+0*x[:,:1] instead of tf.constant(1.0)
z = tf.concat((1.0+0*x[:,:1],x,y),axis=1)

new_input = [z[:,i]*z[:,j] for i in range(3+7-1) for j in range(i,3+7)]    
new_input = tf.stack(new_input,1)