我是Tensorflow中的新手,请尝试以下给出的线性回归代码:
{!! Form::model($trade,['method'=>'PATCH', 'action'=> ['trades\AddSingleTradeController@update',$trade->id]]) !!}
<div class="col-sm-10">
<div class="form-group col-sm-5">
{!! Form::label('exchange_id', 'Exchanges:') !!}
{!! Form::select('exchange_id', [''=>'Choose Options'] + $exchanges , null, ['class'=>'form-control'])!!}
</div>
<div class="form-group col-sm-5">
{!! Form::label('market_id', 'Markets:') !!}
{!! Form::select('market_id', [''=>'Choose Options'] + $markets, null, ['class'=>'form-control'])!!}
</div>
<div class="form-group col-sm-10">
{!! Form::label('symbol_id', 'Symbols:') !!}
{!! Form::select('symbol_id', [''=>'Choose Options']+ $symbals , null, ['class'=>'form-control'])!!}
</div>
<div class="form-group col-sm-10">
{{ Form::radio('is_action', 1) }} Buy
{{ Form::radio('is_action', 0) }} Sell
</div>
<div class="form-group col-lg-5">
{!! Form::label('tradedate', 'Traded date:') !!}
{!! Form::date('tradedate', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group col-lg-5">
{!! Form::label('rate', 'Traded Rate:') !!}
{!! Form::text('rate', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group col-sm-10">
{!! Form::label('reason', 'Choose Reasons:') !!}
{{Form::select('reason',$reasons,null, array('id'=>'reasons','multiple'=>'multiple','name'=>'reason[]',"class"=>"js-example-basic-multiple form-control", 'data-width'=>'60%', 'data-live-search'=>'true','onchange' => 'all_function()'))}}
</div>
<div class="form-group col-lg-10">
{!! Form::label('note', 'Note:') !!}
{!! Form::textarea('note', null, ['class'=>'form-control', 'rows' => 2, 'cols' => 40])!!}
</div>
<div class="form-group col-lg-4">
{!! Form::submit('Save', ['class'=>'btn btn-success btn-lg']) !!}
</div>
{!! Form::close() !!}
<div class="form-group col-lg-4">
{!! Form::open(['method'=>'DELETE', 'action'=> ['trades\AddSingleTradeController@destroy', $trade->id]]) !!}
<div class="form-group">
{!! Form::submit('Delete', ['class'=>'btn btn-danger btn-lg']) !!}
</div>
</div>
{!! Form::close() !!}
当循环运行时:
<td><a href="{{route('member.add-single-trade.edit', $trade->id)}}">{{$trade->stoploss}}</a></td>
给我错误
我真的不明白为什么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)
W=tf.Variable(0.0)
b=tf.Variable(0.0)
#print(bd.shape[1])
tf.summary.histogram('Weights', W)
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
X=tf.placeholder(tf.float32, shape=(None,bd.shape[1]-1))
Y=tf.placeholder(tf.float32, shape=(None,1))
Y_=W*X+b
print(X.shape)
print(Y.shape)
loss=tf.reduce_mean(tf.square(Y_-Y))
tf.summary.scalar('loss',loss)
optimizer=tf.train.GradientDescentOptimizer(0.5)
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})
sess.run(loss)
writer.add_summary(s_mer,i)
#tf.summary.histogram(name="loss",values=loss)
if(i%5==0):
print(i, sess.run([W,b]))
wb_.append(sess.run([W,b]))
print(writer.get_logdir())
print(writer.close())
出现在上面的行中。
如果不存在该怎么办?
此外,为什么我们将 s_mer=sess.run(summary_merge,feed_dict={X:dataset_input,Y:dataset_output})
传递到add_summary()中?我不了解它的作用。我们不能只将“ summary_merge”传递到add_summary()中。
请说明add_summary()的作用。
我在IPython NB中遇到的错误是:
feed_dict
答案 0 :(得分:1)
问题在于变量dataset_input
和dataset_output
的类型为float64
,但您使用占位符float32
,但它们必须相同。 dataset_input = np.array(dataset_input, dtype=np.float32)
和dataset_output = np.array(dataset_output, dtype=np.float32)
对我的计算机有帮助。
然后,在调用sess.run(loss)
时,您还需要添加feed_dict={X:dataset_input,Y:dataset_output}
,因为它使用X和Y占位符。
我真的不明白为什么在上一行中存在feed_dict。如果不存在会发生什么?
您需要使用feed_dict将数据提供给占位符。如果您不向其提供任何数据,则会发生类似的错误,因为它没有用于计算的数据。
希望有帮助。