这就是我定义神经网络的方法
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
我需要先将预处理输入x
和y
(两个占位符)用于特征,然后再将它们输入网络。更具体地说,我想使用二次特征,即
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
?
答案 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
将此扩展到x
和y
具有批量维度的情况:
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)