用tflearn逼近正弦函数

时间:2016-10-10 16:56:41

标签: python machine-learning neural-network tensorflow

我正在尝试使用tflearn进行一个非常简单的近似正弦函数,灵感来自this论文。

import tflearn 
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


# generate cosine function
x = np.linspace(-np.pi,np.pi,10000)
y = np.sin(x)



# Network building
net = tflearn.input_data(shape=[10,10000])
net = tflearn.fully_connected(net, 1000)
net = tflearn.layers.core.activation (net, activation='relu')
net = tflearn.regression(net)


# Define model
model = tflearn.DNN(net)
# Start training (apply gradient descent algorithm)
model.fit(x, y,batch_size=10)

但我一直在遇到

  

ValueError:无法为Tensor u' InputData / X:0'提供形状值(10,),其形状为'(?,10,10000)'

错误。

关于我哪里出错的任何想法?

谢谢!

1 个答案:

答案 0 :(得分:0)

更新:我没有为x = np.linspace(-np.pi,np.pi,10000)张量指定形状:

通过将行更改为np.linspace(-np.pi,np.pi,10000).reshape(-1, 1)

来解决(@lejlot)

在行input_data(shape=[10,10000])中,每个输入张量的形状实际上是[None,1],因此将此行更改为net = tflearn.input_data(shape = [None,1])最终解决了问题