我正在尝试在Tensorflow中构建一个简单的线性回归模型。我不明白“无”东西从哪里来?
我已经检查了有关此类问题的一些答案,但不了解导致此错误的主要原因。
“ s_mer”是否获得None值,如果是,那么为什么? 还会弹出此错误的其他情况吗?
代码如下:
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
boston=load_boston()
type(boston)
boston.feature_names
bd=pd.DataFrame(data=boston.data,columns=boston.feature_names)
bd['Price']=pd.DataFrame(data=boston.target)
np.random.shuffle(bd.values)
W0=tf.Variable(0.0000000003)
W1=tf.Variable(0.000000000002)
b=tf.Variable(0.0000000000001)
#print(bd.shape[1])
#tf.summary.histogram('Weights', W0)
#tf.summary.histogram('Weights', W1)
#tf.summary.histogram('Biases', b)
dataset_input=bd.iloc[:, 0 : bd.shape[1]-1];
#dataset_input.head(2)
dataset_output=bd.iloc[:, bd.shape[1]-1]
dataset_output=dataset_output.values
dataset_output=dataset_output.reshape((bd.shape[0],1)) #converted (506,) to (506,1) because in pandas
#the shape was not changing and it was needed later in feed_dict
dataset_input=dataset_input.values #only dataset_input is in DataFrame form and converting it into np.ndarray
# ADDED
dataset_input = np.array(dataset_input, dtype=np.float32)
# ADDED
dataset_output = np.array(dataset_output, dtype=np.float32)
X=tf.placeholder(tf.float32, shape=(None,bd.shape[1]-1))
Y=tf.placeholder(tf.float32, shape=(None,1))
Y_=W0*X*X + W1*X + b
#Y_pred = tf.add(tf.multiply(tf.pow(X, pow_i), W), Y_pred)
print(X.shape)
print(Y.shape)
loss=tf.reduce_mean(tf.square(Y_-Y))
#tf.summary.scalar('loss',loss)
optimizer=tf.train.GradientDescentOptimizer(0.0000000000001)
train=optimizer.minimize(loss)
init=tf.global_variables_initializer()#tf.global_variables_initializer()#tf.initialize_all_variables()
sess=tf.Session()
sess.run(init)
wb_=[]
with tf.Session() as sess:
summary_merge = tf.summary.merge_all()
writer=tf.summary.FileWriter("Users/ajay/Documents",sess.graph)
epochs=10
sess.run(init)
for i in range(epochs):
s_mer=sess.run(summary_merge,feed_dict={X: dataset_input, Y: dataset_output}) #ERROR________ERROR
sess.run(train,feed_dict={X:dataset_input,Y:dataset_output})
#CHANGED
print("loss",sess.run(loss, feed_dict={X:dataset_input,Y:dataset_output}))
#sess.run(loss, feed_dict={X:dataset_input,Y:dataset_output})
writer.add_summary(s_mer,i)
#tf.summary.histogram(name="loss",values=loss)
if(i%5==0):
print(i, sess.run([W0,W1,b]))
wb__.append(sess.run([W0,W1,b]))
print(writer.get_logdir())
print(writer.close())
并出现以下错误:
(?, 13)
(?, 1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-186c4033eb74> in <module>()
73
74 for i in range(epochs):
---> 75 s_mer=sess.run(summary_merge,feed_dict={X: dataset_input, Y: dataset_output}) #ERROR________ERROR
76 sess.run(train,feed_dict={X:dataset_input,Y:dataset_output})
77
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
776 try:
777 result = self._run(None, fetches, feed_dict, options_ptr,
--> 778 run_metadata_ptr)
779 if run_metadata:
780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
967
968 # Create a fetch handler to take care of the structure of fetches.
--> 969 fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
970
971 # Run request and get response.
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in __init__(self, graph, fetches, feeds)
406 """
407 with graph.as_default():
--> 408 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
409 self._fetches = []
410 self._targets = []
~/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/tensorflow/python/client/session.py in for_fetch(fetch)
225 if fetch is None:
226 raise TypeError('Fetch argument %r has invalid type %r' %
--> 227 (fetch, type(fetch)))
228 elif isinstance(fetch, (list, tuple)):
229 # NOTE(touts): This is also the code path for namedtuples.
TypeError: Fetch argument None has invalid type <class 'NoneType'>
答案 0 :(得分:2)
您没有任何摘要(它们都已被注释掉)。在这种情况下,merge_all
返回None
,如in the docs所示。使用merge_all
之前,请添加一些实际的摘要。