NO.51-----调用百度地图api画热力图

本文介绍了一种利用Python和百度API实现地图数据可视化的方案,通过获取特定地区的经济和人口数据,将其转化为地理坐标并绘制在地图上,形成直观的热力图展示。此方法适用于金融研究、地理数据分析等领域。

            在帮助一个金融系的同学做论文时,他提出可以在地图上显示某地区各县市经济和人口数据的对比,想到了调用百度api接口。       

一、获取坐标

      写一个python小程序获取地区坐标

# -*- coding: utf-8 -*-
"""
Created on Tue May 29 21:53:47 2018
@author: slash
DN: 灯光数据
fin: 财政支出
inc: 收入
number: 人口
"""
 
 
import json
from urllib.request import urlopen, quote
import xlrd

# 打开xlsx
book =xlrd.open_workbook("/Users/Macx/Documents/people/heat_map.xlsx")
# 打开sheet1
sh = book.sheet_by_index(0)
# 待拼接的url(不完整的url)
url = 'http://api.map.baidu.com/geocoder/v2/'
ak = 'AAmi3duoYrCIxfPR0Gx51HRfTDaDLIEh'
# 遍历每一行,为什么从1开始呢,因为0是标题行,不需要
for i in range(1, sh.nrows):
    # 各个地区前面加上‘上海市’,保证后面检索到的所有经纬度都是上海市的地区,而不是别的省市的
    city ='普洱市'+sh.cell_value(i,0)
    # 取第二列所有人口
    population = sh.cell_value(i,1)
    # 取第三列所有GDP
    GDP = sh.cell_value(i,2)
    # 因为出现中文,所以转换一下格式
    add = quote(city)
    # 以 json 的格式输出
    output = 'json'
    # URL 正式拼接,这一句画,下面还会详解解释
    uri = url + '?' + 'address=' + add + '&output=' + output + '&ak=' + ak
    req = urlopen(uri)
    # 传入的字符串,需要解码
    res = req.read().decode()
    # 写成json 字典的形式
    temp = json.loads(res)
    # 提取经度
    lng = temp['result']['location']['lng']
    # 提取纬度
    lat = temp['result']['location']['lat']
    # 写成新的字典
    str_temp = '{"lng":' + str(lng) + ',"lat":' + str(lat) + ',"count":' + str(population) +',"GDP":' + str(GDP) +'},'
    print(str_temp)

二、调用百度api 画图

<!DOCTYPE html>
<html lang="en">
<head>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=YizftfScwcArGG1GsDyFpBLH5AuShE0G"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js"></script>
    <title>热力图功能示例</title>
    <style type="text/css">
        ul,li{list-style: none;margin:0;padding:0;float:left;}
        html{height:100%}
        body{height:100%;margin:0px;padding:0px;font-family:"微软雅黑";}
    #container{height:100%;width:100%;}
    #r-result{width:100%;}
    </style>
</head>
<body>
    <div id="container"></div>
    <div id="r-result" style="display:none">
        <input type="button"  onclick="openHeatmap();" value="显示热力图"/><input type="button"  onclick="closeHeatmap();" value="关闭热力图"/>
    </div>
</body>
</html>
<script type="text/javascript">
    var map = new BMap.Map("container");          // 创建地图实例
    
    var point = new BMap.Point(100.97281,22.831387);
    map.centerAndZoom(point, 15);             // 初始化地图,设置中心点坐标和地图级别
    map.setCurrentCity("普洱");        //设置当前显示城市
    map.enableScrollWheelZoom(); // 允许滚轮缩放
    
    
    
    var points =[
                 
                 {"lng":100.98355510297533,"lat":22.79249798435956,"count":3158},
                 {"lng":101.05244246946019,"lat":23.054590179481025,"count":1935},
                 {"lng":101.6985835615522,"lat":23.43772577225623,"count":3691},
                 {"lng":100.83848864452241,"lat":24.451863775494918,"count":3693},
                 {"lng":100.70945658009512,"lat":23.503204071241473,"count":2998},
                 {"lng":100.98205355627458,"lat":23.94714236146736,"count":2132},
                 {"lng":101.86847918429268,"lat":22.59168385943026,"count":1278},
                 {"lng":99.93858828323933,"lat":22.561831918086543,"count":5009},
                 {"lng":99.4780679910718,"lat":22.294225262449547,"count":142},
                 {"lng":99.59662215028884,"lat":22.650656010974465,"count":956},
                 
                 
                 
                 ];//这里面添加经纬度a
        
        
                 if(!isSupportCanvas()){
                     alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~')
                 }
//详细的参数,可以查看heatmap.js的文档 https://github.com/pa7/heatmap.js/blob/master/README.md
    //参数说明如下:
    /* visible 热力图是否显示,默认为true
        * opacity 热力的透明度,1-100
            * radius 势力图的每个点的半径大小
                * gradient  {JSON} 热力图的渐变区间 . gradient如下所示
                    *  {
                        .2:'rgb(0, 255, 255)',
                            .5:'rgb(0, 110, 255)',
                                .8:'rgb(100, 0, 255)'
                                    }
                                        其中 key 表示插值的位置, 0~1.
                                            value 为颜色值.
                                                */
                                            heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":40,"visible":true});
                                            map.addOverlay(heatmapOverlay);
heatmapOverlay.setDataSet({data:points,max:100});

    //closeHeatmap();
    
    
    
    //判断浏览区是否支持canvas
    function isSupportCanvas(){
        var elem = document.createElement('canvas');
        return !!(elem.getContext && elem.getContext('2d'));
    }

    function setGradient(){
        /*格式如下所示:
        {
            0:'rgb(102, 255, 0)',
            .5:'rgb(255, 170, 0)',
            1:'rgb(255, 0, 0)'
        }*/
        var gradient = {};
        var colors = document.querySelectorAll("input[type='color']");
        colors = [].slice.call(colors,0);
        colors.forEach(function(ele){
                       gradient[ele.getAttribute("data-key")] = ele.value;
                       });
heatmapOverlay.setOptions({"gradient":gradient});
}
    
    function openHeatmap(){
        heatmapOverlay.show();
    }
    
    function closeHeatmap(){
        heatmapOverlay.hide();
}
</script>
</body>
</html>

     因为数据差异不明显,看起来并不直观。 

2025-07-27 06:34:49.050571: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered WARNING: All log messages before absl::InitializeLog() is called are written to STDERR E0000 00:00:1753598089.235406 36 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered E0000 00:00:1753598089.287919 36 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb.npz 17464789/17464789 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step 数据集形状: 训练集: (20000, 128) (20000,) 验证集: (5000, 128) (5000,) 测试集: (25000, 128) (25000,) I0000 00:00:1753598105.743405 36 gpu_device.cc:2022] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 15513 MB memory: -> device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0 Model: "functional_1" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ input_layer (InputLayer) │ (None, 128) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ embedding (Embedding) │ (None, 128, 64) │ 640,000 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ lite_transformer │ (None, 128, 64) │ 33,472 │ │ (LiteTransformer) │ │ │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ global_average_pooling1d │ (None, 64) │ 0 │ │ (GlobalAveragePooling1D) │ │ │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_2 (Dense) │ (None, 1) │ 65 │ └─────────────────────────────────┴────────────────────────┴───────────────┘ Total params: 673,537 (2.57 MB) Trainable params: 673,537 (2.57 MB) Non-trainable params: 0 (0.00 B) Epoch 1/15 WARNING: All log messages before absl::InitializeLog() is called are written to STDERR I0000 00:00:1753598112.016146 94 service.cc:148] XLA service 0x7ef2ec012540 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices: I0000 00:00:1753598112.016592 94 service.cc:156] StreamExecutor device (0): Tesla P100-PCIE-16GB, Compute Capability 6.0 I0000 00:00:1753598112.533355 94 cuda_dnn.cc:529] Loaded cuDNN version 90300 36/313 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5287 - loss: 0.6993 I0000 00:00:1753598116.120238 94 device_compiler.h:188] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process. 313/313 ━━━━━━━━━━━━━━━━━━━━ 15s 21ms/step - accuracy: 0.6466 - loss: 0.6406 - val_accuracy: 0.8130 - val_loss: 0.4329 - learning_rate: 1.0000e-04 Epoch 2/15 313/313 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.8495 - loss: 0.3622 - val_accuracy: 0.8554 - val_loss: 0.3257 - learning_rate: 1.0000e-04 Epoch 3/15 313/313 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9026 - loss: 0.2480 - val_accuracy: 0.8598 - val_loss: 0.3216 - learning_rate: 1.0000e-04 Epoch 4/15 313/313 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9239 - loss: 0.2061 - val_accuracy: 0.8616 - val_loss: 0.3358 - learning_rate: 1.0000e-04 Epoch 5/15 313/313 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9342 - loss: 0.1736 - val_accuracy: 0.8578 - val_loss: 0.3611 - learning_rate: 1.0000e-04 Epoch 6/15 313/313 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9529 - loss: 0.1365 - val_accuracy: 0.8586 - val_loss: 0.3935 - learning_rate: 5.0000e-05 测试集准确率: 0.8540, 测试集损失: 0.3377 /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 35757 (\N{CJK UNIFIED IDEOGRAPH-8BAD}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 32451 (\N{CJK UNIFIED IDEOGRAPH-7EC3}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 36718 (\N{CJK UNIFIED IDEOGRAPH-8F6E}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 27425 (\N{CJK UNIFIED IDEOGRAPH-6B21}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 25439 (\N{CJK UNIFIED IDEOGRAPH-635F}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 22833 (\N{CJK UNIFIED IDEOGRAPH-5931}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 26354 (\N{CJK UNIFIED IDEOGRAPH-66F2}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 39564 (\N{CJK UNIFIED IDEOGRAPH-9A8C}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 35777 (\N{CJK UNIFIED IDEOGRAPH-8BC1}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 20934 (\N{CJK UNIFIED IDEOGRAPH-51C6}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 30830 (\N{CJK UNIFIED IDEOGRAPH-786E}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:135: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from current font. plt.tight_layout() /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 35757 (\N{CJK UNIFIED IDEOGRAPH-8BAD}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 32451 (\N{CJK UNIFIED IDEOGRAPH-7EC3}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 36718 (\N{CJK UNIFIED IDEOGRAPH-8F6E}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 27425 (\N{CJK UNIFIED IDEOGRAPH-6B21}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 25439 (\N{CJK UNIFIED IDEOGRAPH-635F}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 22833 (\N{CJK UNIFIED IDEOGRAPH-5931}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 26354 (\N{CJK UNIFIED IDEOGRAPH-66F2}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 39564 (\N{CJK UNIFIED IDEOGRAPH-9A8C}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 35777 (\N{CJK UNIFIED IDEOGRAPH-8BC1}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 20934 (\N{CJK UNIFIED IDEOGRAPH-51C6}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 30830 (\N{CJK UNIFIED IDEOGRAPH-786E}) missing from current font. plt.savefig('training_history.png') /tmp/ipykernel_36/130590330.py:136: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from current font. plt.savefig('training_history.png') /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 25439 (\N{CJK UNIFIED IDEOGRAPH-635F}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 22833 (\N{CJK UNIFIED IDEOGRAPH-5931}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 26354 (\N{CJK UNIFIED IDEOGRAPH-66F2}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 35757 (\N{CJK UNIFIED IDEOGRAPH-8BAD}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 32451 (\N{CJK UNIFIED IDEOGRAPH-7EC3}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 36718 (\N{CJK UNIFIED IDEOGRAPH-8F6E}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 27425 (\N{CJK UNIFIED IDEOGRAPH-6B21}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 39564 (\N{CJK UNIFIED IDEOGRAPH-9A8C}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 35777 (\N{CJK UNIFIED IDEOGRAPH-8BC1}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20934 (\N{CJK UNIFIED IDEOGRAPH-51C6}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 30830 (\N{CJK UNIFIED IDEOGRAPH-786E}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) 782/782 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 36127 (\N{CJK UNIFIED IDEOGRAPH-8D1F}) missing from current font. plt.savefig('confusion_matrix.png') /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 38754 (\N{CJK UNIFIED IDEOGRAPH-9762}) missing from current font. plt.savefig('confusion_matrix.png') /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 35780 (\N{CJK UNIFIED IDEOGRAPH-8BC4}) missing from current font. plt.savefig('confusion_matrix.png') /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 35770 (\N{CJK UNIFIED IDEOGRAPH-8BBA}) missing from current font. plt.savefig('confusion_matrix.png') /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 27491 (\N{CJK UNIFIED IDEOGRAPH-6B63}) missing from current font. plt.savefig('confusion_matrix.png') /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 28151 (\N{CJK UNIFIED IDEOGRAPH-6DF7}) missing from current font. plt.savefig('confusion_matrix.png') /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 28102 (\N{CJK UNIFIED IDEOGRAPH-6DC6}) missing from current font. plt.savefig('confusion_matrix.png') /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 30697 (\N{CJK UNIFIED IDEOGRAPH-77E9}) missing from current font. plt.savefig('confusion_matrix.png') /tmp/ipykernel_36/130590330.py:150: UserWarning: Glyph 38453 (\N{CJK UNIFIED IDEOGRAPH-9635}) missing from current font. plt.savefig('confusion_matrix.png') <Figure size 600x600 with 0 Axes> /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 36127 (\N{CJK UNIFIED IDEOGRAPH-8D1F}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 38754 (\N{CJK UNIFIED IDEOGRAPH-9762}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 35780 (\N{CJK UNIFIED IDEOGRAPH-8BC4}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 35770 (\N{CJK UNIFIED IDEOGRAPH-8BBA}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 27491 (\N{CJK UNIFIED IDEOGRAPH-6B63}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 28151 (\N{CJK UNIFIED IDEOGRAPH-6DF7}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 28102 (\N{CJK UNIFIED IDEOGRAPH-6DC6}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 30697 (\N{CJK UNIFIED IDEOGRAPH-77E9}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) /usr/local/lib/python3.11/dist-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 38453 (\N{CJK UNIFIED IDEOGRAPH-9635}) missing from current font. fig.canvas.print_figure(bytes_io, **kw) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /tmp/ipykernel_36/130590330.py in <cell line: 0>() 205 206 # 可视化一个样本的注意力 --> 207 visualize_attention(model, sample_idx=42) /tmp/ipykernel_36/130590330.py in visualize_attention(model, sample_idx) 154 def visualize_attention(model, sample_idx): 155 # 创建返回注意力权重的模型 --> 156 attention_output = model.layers[2].attn.output[1] # 获取注意力权重 157 attention_model = keras.Model( 158 inputs=model.input, /usr/local/lib/python3.11/dist-packages/keras/src/ops/operation.py in output(self) 264 Output tensor or list of output tensors. 265 """ --> 266 return self._get_node_attribute_at_index(0, "output_tensors", "output") 267 268 def _get_node_attribute_at_index(self, node_index, attr, attr_name): /usr/local/lib/python3.11/dist-packages/keras/src/ops/operation.py in _get_node_attribute_at_index(self, node_index, attr, attr_name) 283 """ 284 if not self._inbound_nodes: --> 285 raise AttributeError( 286 f"The layer {self.name} has never been called " 287 f"and thus has no defined {attr_name}." AttributeError: The layer multi_head_attention has never been called and thus has no defined output.
最新发布
07-28
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值