【发布时间】:2018-05-08 15:09:51
【问题描述】:
这是我的第一个深度神经网络,我的代码有问题 在实施时。 除了错误之外,代码很慢,这是另一回事。
代码如下:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD, Adam
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
x, y = make_moons(n_samples=1000, noise=0.1, random_state=0)
plt.plot(x[y == 0, 0], x[y == 0, 1], 'ob', alpha=0.5)
plt.plot(x[y == 1, 0], x[y == 1, 1], 'xr', alpha=0.5)
plt.legend(['0', '1'])
plt.show()
print(x.shape)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.3, random_state=42)
model = Sequential
model.add(Dense(1, input_shape=(2,), activation='sigmoid'))
model.compile(Adam(lr=0.5), 'binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train,epochs=200, verbose=0)
results = model.evaluate(x_test, y_test)
print('The Accuracy score on the Train set is:\t{:0.3f}'.format(results[1]))
这是错误
> Using TensorFlow backend. 2017-11-24 17:03:32.402292: W
> C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but
> these are available on your machine and could speed up CPU
> computations. 2017-11-24 17:03:32.402768: W
> C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but
> these are available on your machine and could speed up CPU
> computations. (1000, 2) Traceback (most recent call last): File
> "E:/Tutorial/Deep Learning.py", line 18, in <module>
> model.add(Dense(1, input_shape=(2,), activation='sigmoid')) TypeError: add() missing 1 required positional argument: 'layer'
【问题讨论】:
-
使用 matplotlib,您需要执行
plt.show()来启动显示绘图的事件循环。但是您似乎还有其他一些错误。 -
错误告诉你问题出在哪里。哪一部分出乎意料?
-
错误信息很清楚问题是什么。你试过解决吗?
-
我确实在文档中查找过,但什么也没有
-
您需要一个
Sequential实例。您还没有使用Sequential()实例化它
标签: python matplotlib neural-network keras sklearn-pandas