部署calico网络插件

安装 Calico 的两种主流方式对比 项目Manifest 安装(calico.yaml)Tigera Operator 安装安装复杂度⭐⭐ 简单⭐⭐⭐ 中等偏高可定制性⭐⭐ 一般(手动改 YAML)⭐⭐⭐⭐ 高(声明式 CRD 配置)升级与维护❌ 手动操作✅ Operator 自动处理推荐场景测试环境、小型集群生产环境、中大型集群官方推荐✅ 较为常用✅✅ 官方强烈推荐快速部署测试:选择Manifest 安装长期稳定运维:推荐使用Tigera Operator + CRD 安装方式。 阅读详情
AI摘要

本文介绍了在Kubernetes环境中部署Calico网络插件的具体步骤,包括Operator方式、Manifest方式及Helm方式等不同安装方法,并提供了问题排查及解决办法。

部署calico网络插件

之前的k8s环境中主要使用了flannel作为网络插件,这次改用calico。calico支持多种安装方式,以下是具体的操作步骤。

1. 准备工作

  1. 环境信息
# 系统信息
root@master1:~# cat /etc/issue
Ubuntu 24.04 LTS \n \l

root@master1:~# uname -r
6.8.0-31-generic

# k8s版本
root@master1:~# kubectl get node
NAME      STATUS     ROLES           AGE    VERSION
master1   NotReady   control-plane   2m2s   v1.28.2
node1     NotReady   <none>          84s    v1.28.2
node2     NotReady   <none>          79s    v1.28.2
  1. 版本配套

以最新的calico v3.28版本为例,适配如下k8s版本,我选用这个版本进行安装。

  • v1.27
  • v1.28
  • v1.29
  • v1.30

参考自:System requirements | Calico Documentation (tigera.io)

2. Operator方式安装

  1. 安装operator
# 下载operator资源清单文件
root@master1:~/calico# wget https://raw.githubusercontent.com/projectcalico/calico/v3.28.0/manifests/tigera-operator.yaml
# 或者
root@master1:~/calico# curl https://raw.githubusercontent.com/projectcalico/calico/v3.28.0/manifests/tigera-operator.yaml -O

# 创建应用资源清单文件,创建operator
root@master1:~/calico# kubectl create -f tigera-operator.yaml
namespace/tigera-operator created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgpfilters.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/caliconodestatuses.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipreservations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/kubecontrollersconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/apiservers.operator.tigera.io created
customresourcedefinition.apiextensions.k8s.io/imagesets.operator.tigera.io created
customresourcedefinition.apiextensions.k8s.io/installations.operator.tigera.io created
customresourcedefinition.apiextensions.k8s.io/tigerastatuses.operator.tigera.io created
serviceaccount/tigera-operator created
clusterrole.rbac.authorization.k8s.io/tigera-operator created
clusterrolebinding.rbac.authorization.k8s.io/tigera-operator created
deployment.apps/tigera-operator created
  1. 创建CRD资源
# 下载CRD资源清单文件
root@master1:~/calico# wget https://raw.githubusercontent.com/projectcalico/calico/v3.28.0/manifests/custom-resources.yaml

#修改custom-resources.yaml的ip
root@master1:~/calico# vim custom-resources.yaml


apiVersion: operator.tigera.io/v1
kind: Installation
metadata:
  name: default
spec:
  # Configures Calico networking.
  calicoNetwork:
    ipPools:
    - name: default-ipv4-ippool
      blockSize: 24			# 改为24,每个节点一个C段地址
      cidr: 10.244.0.0/16		# 与kubeadm初始化时"--pod-network-cidr=10.244.0.0/16"保持一致
      encapsulation: VXLANCrossSubnet
      natOutgoing: Enabled
      nodeSelector: all()


# 创建应用资源清单文件,创建operator
root@master1:~/calico# kubectl create -f custom-resources.yaml
installation.operator.tigera.io/default created
apiserver.operator.tigera.io/default created

  1. 确认安装的pod

watch kubectl get pods -n calico-system

问题记录

  1. node节点NotReady
root@master1:~/calico# kubectl get pod -n calico-system
NAME                                      READY   STATUS    RESTARTS      AGE
calico-kube-controllers-b7fb9d96c-pbf9s   1/1     Running   0             47m
calico-node-gdsjw                         1/1     Running   2 (16m ago)   47m
calico-node-hvqg4                         1/1     Running   0             15m
calico-node-nntpd                         0/1     Running   0             42s
calico-typha-55ccdf44bf-v2zmm             1/1     Running   0             15m
calico-typha-55ccdf44bf-w5l8w             1/1     Running   0             47m
csi-node-driver-bqvb7                     2/2     Running   0             47m
csi-node-driver-cw59h                     2/2     Running   0             47m
csi-node-driver-hbw2n                     2/2     Running   0             47m

Warning  Unhealthy  55s (x2 over 56s)  kubelet            Readiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to connect to BIRDv4 socket: dial unix /var/run/calico/bird.ctl: connect: connection refused
  Warning  Unhealthy  28s                kubelet            Readiness probe failed: 2024-07-01 08:57:58.226 [INFO][267] confd/health.go 202: Number of node(s) with BGP peering established = 2
calico/node is not ready: felix is not ready: Get "http://localhost:9099/readiness": dial tcp: lookup localhost on 8.8.8.8:53: no such host

解决:

root@master1:~/calico# vi custom-resources.yaml
...
spec:
  # Configures Calico networking.
  calicoNetwork:
    ipPools:
    - name: default-ipv4-ippool
      blockSize: 24
      cidr: 10.244.0.0/16
      encapsulation: VXLANCrossSubnet
      natOutgoing: Enabled
      nodeSelector: all()
    nodeAddressAutodetectionV4:		#增加的配置
      interface: ens*

# 更新
root@master1:~/calico# kubectl apply -f custom-resources.yaml

# 同时修改节点的dns
root@node1:~# cat /etc/netplan/01-concfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: no
      addresses:
        - 192.168.0.62/24
      routes:
        - to: 0.0.0.0/0
          via: 192.168.0.1
      nameservers:
        addresses:
          - 223.5.5.5
          - 223.6.6.6

# 更改生效
root@node1:~# netplan apply

3. Manifest方式安装

根据node节点数量和使用的db选择如下之一方式进行安装:

以小于50个node,使用Kubernetes API datastore为例:

  1. 下载使用 Kubernetes API datastore的manifest

    curl https://raw.githubusercontent.com/projectcalico/calico/v3.28.0/manifests/calico.yaml -O
    
  2. 修改Pod CIDR

    取消注释CALICO_IPV4POOL_CIDR,修改Pod CIDR。

  3. 基于YAML进行创建

kubectl apply -f calico.yaml

4. helm方式安装

本次部署安装calico作为k8s的网络插件,使用helm进行安装:

# 下载安装helm
wget https://get.helm.sh/helm-v3.15.2-linux-amd64.tar.gz
tar xf helmelm-v3.15.2-linux-amd64.tar.gz
cd linux-amd64/helm /usr/local/bin/helm
helm version

安装calico:

# 创建tigera-operator命名空间
kubectl create namespace tigera-operator
# 安装tigera calico operator ,创建crd资源
helm install calico projectcalico/tigera-operator --version v3.28.0 --namespace tigera-operator
# 确认相关pod运行正常
watch kubectl get pods -n calico-system

准备工作

  • 已经按照helm3
  • 已经按照k8s环境
  • 已经配置完成kubeconfig
  • Calico可以管理主机上的cali和tunl接口。如果使用了NetworkManager,参考 Configure NetworkManager.

安装

  1. 添加calico helm repo:
helm repo add projectcalico https://docs.tigera.io/calico/charts

如果要自定义chart相关的参数,可以配置values.yaml

cat > values.yaml <<EOF
installation:
  kubernetesProvider: AKS
  cni:
    type: Calico
  calicoNetwork:
    bgp: Disabled
    ipPools:
    - cidr: 10.244.0.0/16
      encapsulation: VXLAN
EOF
  1. 创建tigera-operator命名空间.
kubectl create namespace tigera-operator
  1. 使用helm chart安装Tigera Calico operator 和CRD
helm install calico projectcalico/tigera-operator --version v3.28.0 --namespace tigera-operator

或者使用values.yaml传递参数值 :

helm install calico projectcalico/tigera-operator --version v3.28.0 -f values.yaml --namespace tigera-operator
  1. 确认pod运行正常
watch kubectl get pods -n calico-system

说明

Tigera operator安装到calico-system命名空间,其他安装方式使用了 kube-system命名空间。

参考:

Install using Helm | Calico Documentation (tigera.io)

5. 参考资料

  1. https://helm.sh/zh/docs/intro/install/
  2. https://github.com/helm/helm/releases/
  3. https://docs.tigera.io/calico/latest/getting-started/kubernetes/
  4. Releases · projectcalico/calico (github.com)
新时期下k8s 网络插件calico 安装 解决cni网络插件calico的安装,解决k8smaster节点notready状态 阅读详情

相关推荐

Kubernetes 入门篇之网络插件 calico 部署与安装

在运行 和 命令部署好和节点后, 看到节点都是状态,这是因为没有安装网络插件。 1. 安装 通过命令 安装:1) 直接通过官方yaml文件安装,国内一般都下载不了,需要翻墙 2) 先下载下来,然后执行, 本文采用的这种方式 2. 下载镜像到本地 因为中的 默认使用的是官方源,国内下载不下来,需要先把镜像拉取到本地,从本地镜像启动。下载地址:calico国内镜像下载地址本文这里把 写成了脚本 , 具体下载版本可以根据需要自行修改。 3. 修改calico.yaml 由于上面 给镜像 就是打的, 所以保证后

青山绿水 4972

kubernetes集群(k8s)之安装部署Calico 网络

kubernetes集群(k8s)之安装部署Calico 网络

共产主义接班人 1万+

K8S部署calico网络插件有一个calico-node-xxx始终0/1

1、情况是这样的,我在master部署calico网络插件的时候,运行完kubectl apply -f calico.yml后,查看pod运行情况: (base) [root@master opt]# kubectl get pod -A NAMESPACE NAME READY STATUS RESTARTS AGE default nfs-client-provis

BOUTTIME的博客 6635

安装最新版Calico

进入calico网站: Install Calico networking and network policy for on-premises deployments 如下找到Calico.yaml的下载连接,执行: curl https://projectcalico.docs.tigera.io/manifests/calico-etcd.yaml -o calico.yaml 下载好calico.yaml. 编辑calico.yaml里的Secret段,data下定义的证书.

wvxvsuizhong的博客 1万+

14. k8s二进制集群之集群网络插件calico部署

Calico作为k8s的网络插件,它支持多种网络模式(如 BGP、Overlay),并提供了强大的网络策略功能,适用于大规模 Kubernetes 集群。

孤独的狼 2225

【kubernetes】二进制部署k8s集群之cni网络插件flannel和calico工作原理(中)

/在 master01 节点上操作#上传 calico.yaml 文件到 /opt/k8s 目录中,部署 CNI 网络#修改里面定义Pod网络(CALICO_IPV4POOL_CIDR),与前面kube-controller-manager配置文件指定的cluster-cidr网段一样#等 Calico Pod 都 Running,节点也会准备就绪---------- node02 节点部署 ----------//在 node01 节点上操作cd /opt/

liu_xueyin的博客 2583

k8s离线环境下部署【calico】网络插件

本文介绍了私有化部署Kubernetes网络插件Calico的详细步骤。Calico作为高性能云原生网络解决方案,相比Flannel等插件具有三层路由、丰富策略能力和BGP支持等优势。文章采用Operator方式安装,通过修改镜像仓库地址实现离线部署,包括下载Operator文件、创建自定义资源配置、应用部署以及镜像补齐等关键环节,最终验证节点Ready状态确认部署成功。该方法适用于企业生产环境,为私有化部署提供了可靠参考方案。

youmiqianyue的博客 1107

K8s(六):网络插件之Calico安装与详解

🔴 K8s(六):网络插件之Calico安装与详解

✨ 欢迎来到【Seal ^_^ 的CSDN博客】!✨ 3万+

【k8s】k8s部署网络插件Calico、创建网络策略

一、简介 01_calico简介 calico官网 flannel实现的是网络通信 , calico的特性是在pod之间的隔离。 通过BGP路由,但大规模端点的拓扑计算和收敛往往需要一定的时间和计算资源。 纯三层的转发,中间没有任何的NAT和overlay,转发效率最好。 Calico仅依赖三层路由可达。Calico 较少的依赖性使它能适配所有VM、Container、白盒或者混合环境场景。 02_calico网络架构 Felix:监听ECTD中心的存储获取事件,用户创建pod后,Felix负责将其网

sl963216757的博客 1万+

K8S 部署网络插件calico

K8S 部署网络插件calico

叱咤少帅的博客 1244

kubernetes+calico安装及总结

kubernetes calico安装及总结,里面会涉及到kubernetes的安全认证、serviceaccount pod认证。

第二篇:kubernetes部署calico网络插件

说明: 总的目标是在k8s集群部署gitlab、jenkins,并且在本地提交代码到gitlab后jenkin流水线可以自动编译打包成为docker镜像然后部署到k8s中并实现客户端外部域名访问,在文档分为多个部分,其中涉及的技术有docker安装、k8s搭建、部署gitlab、部署jenkins、部署sonarqube、gitlab和jenkin联动、jenkins和sonarqube联动、pipline脚本编写、istio部署、istio服务网关等…此文档接。

kjkdd的博客 5755

BLDC方波控制双闭环Simulink仿真模型

无刷直流电机方波控制双闭环Simulink仿真模型搭建详细教程,包括速度PI环、电流PI环、换向逻辑和受控电压源,仿真结果良好。

m0_57458432的博客 6882

kubernetes系列文章六:网络插件calico部署

kuberentes部署calico网络插件

zxy1981742484的博客 451

用Docker-Compose三节点部署生产级Doris集群:从配置文件到故障转移

本文详细介绍了如何使用Docker-Compose部署一个生产级的三节点Apache Doris集群。内容涵盖从核心架构理解、配置文件编写到集群初始化验证的全过程,并重点讲解了如何实现高可用、数据持久化、健康检查以及故障转移等生产级运维关键点,为中小团队提供了一种在容器化便利性与系统稳定性间取得平衡的可靠部署方案。

kkk56的博客 864

1104全部报表版本号-银监会报表

G4B_Ⅲ(a)X1《与非中央交易对手的衍生具交易对手信用风险违约风险计算表(现期风险暴露法)(第一档)》G4B_Ⅲ(b)X1《与非中央交易对手的证券融资交易对手信用风险违约风险计算表(权重法)(第一档)》G4B_Ⅲ(a1)X2《与非中央交易对手的衍生具交易对手信用风险违约风险计算表(标准法)(第二档)》Ⅲ(a)X2《与非中央交易对手的衍生具交易对手信用风险违约风险计算表(现期风险暴露法)(第二档)》Ⅲ(a1)X1《与非中央交易对手的衍生具交易对手信用风险违约风险计算表(标准法)(第一档)》

数字化老司机 6338

09.kubernetes 部署calico / flannel网络插件

脚本中实现了 calico 和 flannel 这两种主流的网络插件,选择其中一种部署即可。

CodingDemo 1318

UFS软件分区介绍

本文主要介绍了ufs软件分区管理的概况,能够使大家对软件分区有个大体的认识

肥匪斐's blogs 1万+

Kubernetes - Calico 网络插件的部署与基础配置

如果你在云环境(如 AWS、阿里云)中部署,建议启用 IPIP 模式,因为这些环境通常不支持二层 BGP 通信。修改文件,搜索,将其值从Never改为Always或者,如果你希望完全自定义 IP Pool,可以删除默认的ippoolmetadata:spec:一旦 Pod 已经创建,修改 IP Pool 不会自动影响已有 Pod。建议在集群初始化时配置好。今天我们系统性地完成了:✅ 理解了 Kubernetes 网络模型与 CNI 的角色✅ 部署了 Calico 网络插件并验证其运行。

千淘万漉虽辛苦,吹尽狂沙始到金 6096

置顶必读(2) | 《YOLOv11实战:从入门到深度优化》目录导航篇(持续更新中)

🏆 本文收录于 《YOLOv11实战:从入门到深度优化》 专栏。 本专栏系统梳理并持续复现全网各类 YOLOv11 / YOLO系列改进方案与实战案例,目前已覆盖 分类、检测、分割、追踪、关键点、OBB 检测 等多个方向。 内容坚持 持续更新 + 深度拆解 + 工程落地导向,不仅讲“怎么改”,更讲“为什么这样改、如何真正落地到项目中”。

**My Coding Family** 11万+

突破Cloudflare 5秒盾!Python爬虫+GPT分析JS混淆实战(附完整代码)

这篇文章的核心观点其实不是“怎么过CF”,而是如何把LLM嵌入逆向工程的工作流。AI不会取代逆向工程师,但它正在重新定义逆向工程师的工作方式。过去我们80%的时间花在读懂混淆代码上,现在这部分可以外包给GPT,我们把精力集中在架构判断、异常处理和系统设计这些真正需要人类直觉的地方。技术会变,CF的防护会升级,GPT的能力也会迭代。但这种“人机协作拆解复杂问题”的方法论,比任何具体的绕过技巧都更有长期价值。参考资料Cloudflare Bot Management官方文档。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 547

云原生|kubernetes|网络插件flannel二进制部署和calico的yaml清单部署总结版

kubernetes的网络选择 通常的,不管是二进制还是kubeadm亦或者其它的集群安装方式,在初始化集群这一步完成后,需要做得第一个事情是搭建集群内部的网络,而在网络没有搭建完成的时候,kubectl查看集群的状态是noready的,也就是说集群是不可用的,没有办法创建任何pod(在云计算里也是一样的,没有网络模型的时候,是无法创建虚拟机的)

zsk_john的技术分享专用博客 2350

低代码组件开发——OpenClaw智能助手的组件设计与开发(2026技术版)

OpenClaw智能助手的低代码组件开发平台采用分层架构设计,包含组件定义、注册、配置、渲染和生命周期管理等核心模块。该系统支持UI、功能、数据和集成四种组件类型,强调可重用性、可配置性和可扩展性。组件定义通过TypeScript、React等技术实现组件结构和行为的描述,组件注册中心基于Node.js和MongoDB管理组件版本。该平台通过React、Redux等技术实现组件渲染和状态管理,并提供完整的生命周期管理机制。这种架构设计显著提升了智能助手应用的开发效率和质量。

一键难忘的博客 409

【Kubernetes】二进制部署k8s集群之cni网络插件flannel和calico

二进制部署k8s

weixin_68840588的博客 1664

【AI智能体】Dify 基于知识库搭建智能客服问答应用详解

Dify 基于知识库搭建智能客服问答应用详解

congge 6937

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: ingress-nginx部署-helm方式
下一篇: calico客户端工具calicoctl
KongTY312
博客等级 码龄12年 2143粉丝 189原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KongTY312

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址