【问题标题】:my first deep network我的第一个深度网络
【发布时间】: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


【解决方案1】:

Sequential 后面缺少括号:

model = Sequential()

如果您想编译 tensorflow 以使用特殊的 x86 指令(例如 AVX),请参阅 How to compile Tensorflow with SSE4.2 and AVX instructions?,但这可能比它付出的努力更多。

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2015-03-30
    • 2020-05-06
    • 1970-01-01
    • 2018-09-25
    • 2019-12-02
    相关资源
    最近更新 更多