这篇文章主要围绕TensorflowSaver似乎会覆盖现有的已保存变量文件和tensorflow保存部分模型展开,旨在为您提供一份详细的参考资料。我们将全面介绍TensorflowSaver似乎会覆
这篇文章主要围绕Tensorflow Saver似乎会覆盖现有的已保存变量文件和tensorflow保存部分模型展开,旨在为您提供一份详细的参考资料。我们将全面介绍Tensorflow Saver似乎会覆盖现有的已保存变量文件的优缺点,解答tensorflow保存部分模型的相关问题,同时也会为您带来Argo(events) 使用 Sensor 触发现有的 ClusterWorkflowTemplate、Effective TensorFlow Chapter 4: TensorFlow中的广播Broadcast机制【转】、Tensorflow 2.0 - AttributeError:模块'tensorflow'没有属性'Session'、Tensorflow 2.0-AttributeError:模块'tensorflow'没有属性'Session'的实用方法。
本文目录一览:- Tensorflow Saver似乎会覆盖现有的已保存变量文件(tensorflow保存部分模型)
- Argo(events) 使用 Sensor 触发现有的 ClusterWorkflowTemplate
- Effective TensorFlow Chapter 4: TensorFlow中的广播Broadcast机制【转】
- Tensorflow 2.0 - AttributeError:模块'tensorflow'没有属性'Session'
- Tensorflow 2.0-AttributeError:模块'tensorflow'没有属性'Session'
Tensorflow Saver似乎会覆盖现有的已保存变量文件(tensorflow保存部分模型)
我在Tensorflow中编写神经网络代码。我做到了在每1000个时代保存变量。因此,我希望为不同的文件保存第1001个纪元,第2001个纪元,第3001个纪元……的变量。下面的代码是我所做的保存功能。
def save(self, epoch): model_name = "MODEL_save" checkpoint_dir = os.path.join(model_name) if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) self.saver.save(self.sess, checkpoint_dir + ''/model'', global_step=epoch) self.saver.save(self.sess, checkpoint_dir + ''/model'') print("path for saved %s" % checkpoint_dir)
调用该函数后,我将该代码保存了两次。因为我想使用’global_step =
epoch’保存每1000个时代的变量历史。并希望将最新的变量保存在文件中而未指定时期。每当满足以下条件时,我都会调用此函数。
for epoch in xrange(self.m_total_epoch): .... CODE FOR NEURAL NETWORK .... if epoch%1000 == 1 and epoch != 1: self.save(epoch)
假设当前纪元是29326,我希望目录中所有已保存的文件都来自1001、2001、3001
…29001。但是,只有26001、27001、28001、29001中的文件存在一部分。我检查了它是否在其他计算机上发生。这与我的预期不同。为什么会发生?
答案1
小编典典tf.train.Saver``max_to_keep
构造函数中有一个参数,该参数仅保留最新模型。max_to_keep
令人惊讶的是,该参数的默认值为5。因此,默认情况下,您只有5个最新模型。
要保留所有模型,请将此变量设置为None
:
saver = tf.train.Saver(max_to_keep=None)
Argo(events) 使用 Sensor 触发现有的 ClusterWorkflowTemplate
如何解决Argo(events) 使用 Sensor 触发现有的 ClusterWorkflowTemplate?
我正在尝试从 argo/argo-events 中的发布请求触发预先存在的 ClusterWorkflowTemplate
。
我一直在关注示例 here,但我不想在传感器中定义工作流程 - 我想将其分开。
我无法让传感器导入并触发工作流程,是什么问题?
# kubectl apply -n argo-test -f templates/whalesay/workflow-template.yml
apiVersion: argoproj.io/v1alpha1
kind: ClusterWorkflowTemplate
Metadata:
name: workflow-template-submittable
spec:
entrypoint: whalesay-template
arguments:
parameters:
- name: message
value: hello world
templates:
- name: whalesay-template
inputs:
parameters:
- name: message
container:
image: docker/whalesay
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
# kubectl apply -n argo-events templates/whalesay/event-source.yml
apiVersion: argoproj.io/v1alpha1
kind: EventSource
Metadata:
name: webhook
spec:
service:
ports:
- port: 12000
targetPort: 12000
webhook:
# event-source can run multiple HTTP servers. Simply define a unique port to start a new HTTP server
example:
# port to run HTTP server on
port: "12000"
# endpoint to listen to
endpoint: /example
# HTTP request method to allow. In this case,only POST requests are accepted
method: POST
# kubectl apply -n argo-events -f templates/whalesay/sensor.yml
apiVersion: argoproj.io/v1alpha1
kind: Sensor
Metadata:
name: workflow
namespace: argo-events
finalizers:
- sensor-controller
spec:
template:
serviceAccountName: operate-workflow-sa
dependencies:
- name: http-post-trigger
eventSourceName: webhook
eventName: example
triggers:
- template:
name: workflow-trigger-1
argoWorkflow:
group: argoproj.io
version: v1alpha1
kind: Workflow
operation: submit
Metadata:
generateName: cluster-workflow-template-hello-world-
spec:
entrypoint: whalesay-template
workflowTemplateRef:
name: cluster-workflow-template-submittable
clusterScope: true
# to launch
curl -d ''{"message":"this is my first webhook"}'' -H "Content-Type: application/json" -X POST http://argo-events-51-210-211-4.nip.io/example
# error
{
"level": "error","ts": 1627655074.716865,"logger": "argo-events.sensor-controller","caller": "sensor/controller.go:69","msg": "reconcile error","namespace": "argo-events","sensor": "workflow","error": "temp ││ late workflow-trigger-1 is invalid: argoWorkflow trigger does not contain an absolute action",}
参考文献:
- special-workflow-trigger.yaml
- cluster workflow templates
- spec
解决方法
我不得不:
- 确保我的服务帐户
operate-workflow-sa
具有集群权限 - 更正我的
sensor.yml
语法 spec
集群权限:
# kubectl apply -f ./k8s/workflow-service-account.yml
apiVersion: v1
kind: ServiceAccount
metadata:
namespace: argo-events
name: operate-workflow-sa
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: operate-workflow-role
# namespace: argo-events
rules:
- apiGroups:
- argoproj.io
verbs:
- "*"
resources:
- workflows
- clusterworkflowtemplates
- workflowtemplates
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: operate-workflow-role-binding
namespace: argo-events
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: operate-workflow-role
subjects:
- kind: ServiceAccount
name: operate-workflow-sa
namespace: argo-events
sensor.yml(注意还为工作流添加了 serviceAccountName
):
apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
name: workflow
namespace: argo-events
finalizers:
- sensor-controller
spec:
template:
serviceAccountName: operate-workflow-sa
dependencies:
- name: http-post-trigger
eventSourceName: webhook
eventName: example
triggers:
# https://github.com/argoproj/argo-events/blob/master/api/sensor.md#triggertemplate
- template:
name: workflow-trigger-1
argoWorkflow:
# https://github.com/argoproj/argo-events/blob/master/api/sensor.md#argoproj.io/v1alpha1.ArgoWorkflowTrigger
group: argoproj.io
version: v1alpha1
resource: Workflow
operation: submit
metadata:
generateName: cluster-workflow-template-hello-world-
source:
resource:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: special-trigger
spec:
serviceAccountName: operate-workflow-sa
entrypoint: whalesay-template
workflowTemplateRef:
name: whalesay-template
clusterScope: true
Effective TensorFlow Chapter 4: TensorFlow中的广播Broadcast机制【转】
本文转载自:https://blog.csdn.net/LoseInVain/article/details/78763303
TensorFlow支持广播机制(Broadcast),可以广播元素间操作(elementwise operations)。正常情况下,当你想要进行一些操作如加法,乘法时,你需要确保操作数的形状是相匹配的,如:你不能将一个具有形状[3, 2]的张量和一个具有[3,4]形状的张量相加。但是,这里有一个特殊情况,那就是当你的其中一个操作数是一个具有单独维度(singular dimension)的张量的时候,TF会隐式地在它的单独维度方向填满(tile),以确保和另一个操作数的形状相匹配。所以,对一个[3,2]的张量和一个[3,1]的张量相加在TF中是合法的。(译者:这个机制继承自numpy的广播功能。其中所谓的单独维度就是一个维度为1,或者那个维度缺失,具体可参考numpy broadcast)。
import tensorflow as tf
a = tf.constant([[1., 2.], [3., 4.]]) b = tf.constant([[1.], [2.]]) # c = a + tf.tile(b, [1, 2]) c = a + b
- 1
- 2
- 3
- 4
- 5
- 6
广播机制允许我们在隐式情况下进行填充(tile),而这可以使得我们的代码更加简洁,并且更有效率地利用内存,因为我们不需要另外储存填充操作的结果。一个可以表现这个优势的应用场景就是在结合具有不同长度的特征向量的时候。为了拼接具有不同长度的特征向量,我们一般都先填充输入向量,拼接这个结果然后进行之后的一系列非线性操作等。这是一大类神经网络架构的共同套路(common pattern)
a = tf.random_uniform([5, 3, 5])
b = tf.random_uniform([5, 1, 6]) # concat a and b and apply nonlinearity tiled_b = tf.tile(b, [1, 3, 1]) c = tf.concat([a, tiled_b], 2) d = tf.layers.dense(c, 10, activation=tf.nn.relu)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
但是这个可以通过广播机制更有效地完成。我们利用事实f(m(x+y))=f(mx+my)f(m(x+y))=f(mx+my),简化我们的填充操作。因此,我们可以分离地进行这个线性操作,利用广播机制隐式地完成拼接操作。
pa = tf.layers.dense(a, 10, activation=None)
pb = tf.layers.dense(b, 10, activation=None)
d = tf.nn.relu(pa + pb)
- 1
- 2
- 3
事实上,这个代码足够通用,并且可以在具有抽象形状(arbitrary shape)的张量间应用:
def merge(a, b, units, activation=tf.nn.relu):
pa = tf.layers.dense(a, units, activation=None) pb = tf.layers.dense(b, units, activation=None) c = pa + pb if activation is not None: c = activation(c) return c
- 1
- 2
- 3
- 4
- 5
- 6
- 7
一个更为通用函数形式如上所述:
目前为止,我们讨论了广播机制的优点,但是同样的广播机制也有其缺点,隐式假设几乎总是使得调试变得更加困难,考虑下面的例子:
a = tf.constant([[1.], [2.]])
b = tf.constant([1., 2.])
c = tf.reduce_sum(a + b)
- 1
- 2
- 3
你猜这个结果是多少?如果你说是6,那么你就错了,答案应该是12.这是因为当两个张量的阶数不匹配的时候,在进行元素间操作之前,TF将会自动地在更低阶数的张量的第一个维度开始扩展,所以这个加法的结果将会变为[[2, 3], [3, 4]],所以这个reduce的结果是12.
(译者:答案详解如下,第一个张量的shape为[2, 1],第二个张量的shape为[2,]。因为从较低阶数张量的第一个维度开始扩展,所以应该将第二个张量扩展为shape=[2,2],也就是值为[[1,2], [1,2]]。第一个张量将会变成shape=[2,2],其值为[[1, 1], [2, 2]]。)
解决这种麻烦的方法就是尽可能地显示使用。我们在需要reduce某些张量的时候,显式地指定维度,然后寻找这个bug就会变得简单:
a = tf.constant([[1.], [2.]])
b = tf.constant([1., 2.])
c = tf.reduce_sum(a + b, 0)
- 1
- 2
- 3
这样,c的值就是[5, 7],我们就容易猜到其出错的原因。一个更通用的法则就是总是在reduce操作和在使用tf.squeeze
中指定维度。
Tensorflow 2.0 - AttributeError:模块'tensorflow'没有属性'Session'
如何解决Tensorflow 2.0 - AttributeError:模块''tensorflow''没有属性''Session''?
根据TF 1:1 Symbols Map
,在 TF 2.0
中,您应该使用tf.compat.v1.Session()
而不是tf.Session()
https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJIteQ/edit#gid=0
要在 TF 2.0 中获得类似 TF 1.x 的行为,可以运行
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
但随后无法从 TF 2.0 中的许多改进中受益。有关更多详细信息,请参阅迁移指南https://www.tensorflow.org/guide/migrate
解决方法
当我sess = tf.Session()
在 Tensorflow 2.0 环境中执行命令时,我收到如下错误消息:
Traceback (most recent call last):
File "<stdin>",line 1,in <module>
AttributeError: module ''tensorflow'' has no attribute ''Session''
系统信息:
- 操作系统平台和发行版:Windows 10
- Python版本:3.7.1
- Tensorflow 版本:2.0.0-alpha0(用 pip 安装)
重现步骤:
安装:
- 点安装——升级点
- pip install tensorflow==2.0.0-alpha0
- 点安装 keras
- pip install numpy==1.16.2
执行:
- 执行命令:import tensorflow as tf
- 执行命令:sess = tf.Session()
Tensorflow 2.0-AttributeError:模块'tensorflow'没有属性'Session'
sess = tf.Session()
在Tensorflow 2.0环境中执行命令时,出现如下错误消息:
Traceback (most recent call last):File "<stdin>", line 1, in <module>AttributeError: module ''tensorflow'' has no attribute ''Session''
系统信息:
- 操作系统平台和发行版:Windows 10
- python版本:3.7.1
- Tensorflow版本:2.0.0-alpha0(随pip一起安装)
重现步骤:
安装:
- 点安装-升级点
- pip install tensorflow == 2.0.0-alpha0
- 点安装keras
- 点安装numpy == 1.16.2
执行:
- 执行命令:将tensorflow导入为tf
- 执行命令:sess = tf.Session()
答案1
小编典典根据TF 1:1 Symbols Map
,在TF 2.0中,您应该使用tf.compat.v1.Session()
而不是tf.Session()
https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJIteQ/edit#gid=0
要获得TF 2.0中类似TF 1.x的行为,可以运行
import tensorflow.compat.v1 as tftf.disable_v2_behavior()
但后来人们无法受益于TF 2.0所做的许多改进。
关于Tensorflow Saver似乎会覆盖现有的已保存变量文件和tensorflow保存部分模型的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Argo(events) 使用 Sensor 触发现有的 ClusterWorkflowTemplate、Effective TensorFlow Chapter 4: TensorFlow中的广播Broadcast机制【转】、Tensorflow 2.0 - AttributeError:模块'tensorflow'没有属性'Session'、Tensorflow 2.0-AttributeError:模块'tensorflow'没有属性'Session'等相关内容,可以在本站寻找。
本文标签: