When Gandalf tells the story of Sméagol to Frodo and reveals that Sméagol became Gollum (I.2), Gandalf say that
“there was something else in it, I think, which you don't see yet. Even Gollum was not wholly ruined. (…) There was a little corner of his mind that was still his own, and light came through it, as through a chink in the dark: light out of the past. It was actually pleasant, I think, to hear a kindly voice again, bringing up memories of wind, and trees, and sun on the grass, and such forgotten things.
“But that, of course, would only make the evil part of him angrier in the end — unless it could be conquered. Unless it could be cured.” Gandalf sighed. “Alas! there is little hope of that for him. Yet not no hope. (…)
He was altogether wretched. He hated the dark, and he hated light more: he hated everything, and the Ring most of all.”
Frodo, at first, doesn't understand how a hobbit may have become Gollum. He isn't even comfortable with Sméagol's murder of Deagol for the ring — “hobbits don't cheat. Gollum meant to cheat all the time”. Frodo regrets that Bilbo didn't kill Gollum, but Gandalf disagrees:
“What a pity that Bilbo did not stab that vile creature, when he had a chance!”
“Pity? It was Pity that stayed his hand. (…)”
“I am sorry,” said Frodo. “But I am frightened; and I do not feel any pity
for Gollum.”
“You have not seen him,” Gandalf broke in.
“No, and I don't want to,” said Frodo. (…) He deserves death.”
“Deserves it! I daresay he does. Many that live deserve death. And some that die deserve life. Can you give it to them? Then do not be too eager to deal out death in judgement. For even the very wise cannot see all ends. I have not much hope that Gollum can be cured before he dies, but there is a chance of it. And he is bound up with the fate of the Ring. My heart tells me that he has some part to play yet, for good or ill, before the end; and when that comes, the pity of Bilbo may rule the fate of many — yours not least.”
Gandalf expands on that later as he hears Faramir's news of having met Frodo, Sam and Gollum trying to enter Mordor (V.4).
“Yet my heart guessed that Frodo and Gollum would meet before the end. For good, or for evil. But of Cirith Ungol I will not speak tonight. Treachery, treachery I fear; treachery of that miserable creature. But so it must be. Let us remember that a traitor may betray himself and do good that he does not intend. It can be so, sometimes.”
In addition to Gandalf's talk, Frodo's world-view may have changed as he realizes that Bilbo, whom he holds in great regard, also lied about the ring — though such an inner journey is speculation on my part, not explicit in the text.
When Frodo and Sam meet Gollum (IV.1), Gollum attacks Sam, and Frodo intervenes with his sword:
“Let go! Gollum,” he said. “This is Sting. You have seen it before once upon a time. Let go, or you'll feel it this time! I'll cut your throat.”
And yet, after Gollum backs down:
“No,” said Frodo. “If we kill him, we must kill him outright. But we can't
do that, not as things are. Poor wretch! He has done us no harm.”
And at this point Frodo remembers the conversation with Gandalf, when Frodo said he had no pity for Gollum, and says that “For now that I see him, I do pity him”.
Gollum promises to serve Frodo, and swears by the Ring. The three journey to the Gate of Mordor, which they find closed (IV.3). Gollum mentions the existence of another way into Mordor.
“Sméagol,” [Frodo] said, “I will trust you once more. Indeed it seems that I must do so, and that it is my fate to receive help from you. where I least looked for it, and your fate to help me whom you long pursued with evil purpose. So far you have deserved well of me and have kept your promise truly. Truly, I say and mean,” he added with a glance at Sam, “for twice now we have been in your power, and you have done no harm to us. Nor have you tried to take from me what you once sought. May the third time prove the best! But I warn you, Sméagol, you are in danger.” (…)
“You swore a promise by what you call the Precious. Remember that! It will hold you to it; but it will seek a way to twist it to your own undoing. Already you are being twisted. You revealed yourself to me just now, foolishly. Give it back to Sméagol you said. (…) You will never get it back. But the desire of it may betray you to a bitter end. (…) If I, wearing it, were to command you, you would obey, even if it were to leap from a precipice or to cast yourself into the fire. And such would be my command. So have a care, Sméagol!”
Sam looked at his master with approval, but also with surprise: there was a look in his face and a tone in his voice that he had not known before. It had always been a notion of his that the kindness of dear Mr. Frodo was of such a high degree that it must imply a fair measure of blindness.
In making Gollum his servant, Frodo feels that he has an obligation to him. (IV.6)
Only one true shot, and Frodo would be rid of the miserable voice for ever. But no, Gollum had a claim on him now. The servant has a claim on the master for service, even service in fear. They would have foundered in the Dead Marshes but for Gollum. Frodo knew, too, somehow, quite clearly that Gandalf would not have wished it.
Faramir's companions capture Gollum, and would kill him, but Frodo guarantees Gollum's life by his own.
When Frodo and Sam confront Shelob (IV.9), Gollum attacks Sam. Frodo doesn't witness the attack, as he is busy trying to evade Shelob. Frodo only learns the attack second-hand (VI.2) and doesn't comment on it. Frodo and Sam next encounter Gollum on the slope of Mount Doom (VI.3), as Frodo already has trouble fighting the Ring's influence. At this point, Sam has carried the Ring — briefly, but in Mordor, where the Ring is more potent. After Gollum's attempt to steal the Ring from Frodo, Sam is in a position to kill him, but demurs.
It would be just to slay this treacherous, murderous creature, just and many times deserved; and also it seemed the only safe thing to do. But deep in his heart there was something that restrained him: he could not strike this thing lying in the dust, forlorn, ruinous, utterly wretched. He himself, though only for a little while, had borne the Ring, and now dimly he guessed the agony of Gollum's shrivelled mind and body, enslaved to that Ring, unable to find peace or relief ever in life again.
So both Sam and Frodo changed their opinion of Gollum after they'd borne the Ring. Gandalf's words were instrumental in Frodo's decision to trust Gollum, but only he met him, and that was after he had been carrying the Ring. Frodo only has very limited trust in Gollum, and that is mostly a trust in how he reveres the Ring rather than a trust on what good remains in him. What both Frodo and Sam come to feel for Gollum is pity.