python - 跨类的张量流命名空间
问题描述
此处提供了该问题的要点:https ://gist.github.com/sankhaMukherjee/97c212072385ee36ebd8bfab9a801794
本质上,我正在尝试生成一个通用类,可以用作我的张量流项目的样板。提供的要点是我能想到的最简单的例子。我知道这与命名空间有关,但不确定如何解决此问题。
我有一个Test
具有以下成员函数的类:
__init__(self, inpShape, outShape, layers, activations)
saveModel(self, sess)
restoreModel(self, sess, restorePoint)
fit(self, X, y, Niter=101, restorePoint=None)
predict(self, X, restorePoint=None)
函数本身很简单,可以在提供的要点中查找。现在,给定这个类,我们可以尝试测试它,看看它是如何做的:
X = np.random.random((10000, 2))
y = (2*X[:, 0] + 3*X[:, 1]).reshape(-1, 1)
inpShape = (None, 2)
outShape = (None, 1)
layers = [7, 1]
activations = [tf.sigmoid, None]
t = Test(inpShape, outShape, layers, activations)
t.fit(X, y, 10000)
yHat = t.predict(X, restorePoint=t.restorePoints[-1])
plt.plot(yHat, y, '.', label='original')
这一切都很好!
现在我们要创建同一个类的另一个实例,并恢复从这里保存的模型。正是在这里,所有的地狱都崩溃了。让我们更新上面的内容以合并:
X = np.random.random((10000, 2))
y = (2*X[:, 0] + 3*X[:, 1]).reshape(-1, 1)
inpShape = (None, 2)
outShape = (None, 1)
layers = [7, 1]
activations = [tf.sigmoid, None]
t = Test(inpShape, outShape, layers, activations)
t.fit(X, y, 10000)
yHat = t.predict(X, restorePoint=t.restorePoints[-1])
plt.plot(yHat, y, '.', label='original')
if True: # In the gist, turn this to True for seeing the problem
t1 = Test(inpShape, outShape, layers, activations)
yHat1 = t1.predict(X, restorePoint=t.restorePoints[-1])
plt.plot(yHat1, y, '.', label='copied')
事实证明,我们不能再这样做了。它会用一个全新的图表和旧的图表完全搞乱一切。现在是否可以创建一个复制旧图的类的新实例,而不是创建旧图的全新实例?
解决方案
推荐阅读
- javascript - 熄灯算法 - 找到打开所有开关的最小开关数量
- java - Derby Database Netbeans,如何防止从数据库中获取数据的表的表单元格被编辑?
- react-native - 如何在 React Native 中实现树状结构 UI?
- artifactory - 如何将一些包从 conan.io/center 迁移到企业 artifactory
- python - QGraphicsView无法拖放两次
- elasticsearch - Elasticsearch计数查询具有超过n次出现另一个值的唯一值
- azure - 启动 Cosmosdb Emulator 时如何传递 2 个参数?
- typescript - 索引签名参数类型必须是“字符串”或“数字”
- python - 在 Sphinx restructuredText 的根目录之外找不到相对路径“../..”:“未知文档错误”
- iframe - 缩放和可拖动的 iframe