qq_42270360 2021-09-28 09:26 采纳率: 100%
浏览 1508
已结题

conv2d(): argument 'input' (position 1) must be Tensor, not Tensor,不知道怎么解决。

import torch
import torch.nn as nn
from tensorflow import keras
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import paddle
import warnings
from paddle.metric import Accuracy

# 训练数据
feature_name1="J:\\multi-scale\\data_test\\data_33.txt"
feature_data1=np.loadtxt(feature_name1)
feature_data1=np.int_(feature_data1)
train_X1=np.reshape(feature_data1,(100,32,32,1))
train_X1 = torch.tensor(train_X1)
print(train_X1.shape)
feature_name2="J:\\multi-scale\\data_test\\data_17.txt"
feature_data2=np.loadtxt(feature_name2)
feature_data2=np.int_(feature_data2)
train_X2=np.reshape(feature_data2,(100,16,16,1))

train_X2 = torch.tensor(train_X2)
print(train_X2.shape)

tag_name="J:\\multi-scale\\data_test\\ch_lable_33.txt"
tag_data=np.loadtxt(tag_name)
train_Y = keras.utils.to_categorical(tag_data, num_classes=8)
print(train_Y.shape)
class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, in_planes, planes, stride=1):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(self.expansion*planes)

        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion*planes:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*planes)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        out = self.bn3(self.conv3(out))
        out += self.shortcut(x)
        out = F.relu(out)
        return out
def _make_layer(in_planes, block, planes, num_blocks, stride):
    strides = [stride] + [1]*(num_blocks-1)
    layers = []
    for stride in strides:
        layers.append(block(in_planes, planes, stride))
        in_planes = planes * block.expansion
    return nn.Sequential(*layers)

#定义网络
input1 = keras.Input(shape=(32, 32,1))
input2 = keras.Input(shape=(16, 16,1))

# Stage 1
x=nn.Conv2d(1, 256, kernel_size=7, stride=2, padding=3, bias=False)(input1)
x=nn.BatchNorm2d(64)(x)
c1 = F.relu(x)
c1 =x= F.max_pool2d(c1, kernel_size=3, stride=2, padding=1)(x)
# Stage 2
c2=_make_layer(Bottleneck, 128, 2, stride=2)(c1)
c3=_make_layer(Bottleneck, 128, 2, stride=2)(c2)
p2=nn.Conv2d( 512, 256, kernel_size=1, stride=1, padding=0)(input2)
#p3=nn.Conv2d( 512, 256, kernel_size=1, stride=1, padding=0)(input3)
p2=nn.Conv2d( 256, 128, kernel_size=1, stride=1, padding=0)(p2)
p3= keras.layers.concatenate([c2, p2])
p3=keras.layers.Flatten()(p3)
p3 = keras.layers.Dense(64, activation='relu')(p3)
output = keras.layers.Dense(8, activation='sigmoid')(p3)
model = keras.Model(inputs=[input1, input2], outputs=output)

model.summary()
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit([train_X1,train_X2],
                    train_Y,
                    epochs=30, validation_split=0.3)

错误提示:

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-黄老师 2021-09-28 09:36
    关注

    x=nn.Conv2d(1, 256, kernel_size=7, stride=2, padding=3, bias=False)(input1)请问(input1)是什么?python语法中好似没有这样的写法

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月17日
  • 已采纳回答 3月9日
  • 创建了问题 9月28日

悬赏问题

  • ¥20 数据结构—复杂度分析
  • ¥15 如何使用单链表编写学生信息管理系统?
  • ¥15 完成课题以及ppt 在21号之前完成
  • ¥15 懂的来,你应该知道我在说什么,我需要你的帮助
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 自己写的测试驱动程序无法启动
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设